当前位置:网站首页>Koa Middleware
Koa Middleware
2022-07-06 17:02:00 【Society, you Lei brother, life is hard, don't bend down】
How middleware works
initialization koa After the instance , We will use use Method to load middleware (middleware), There will be an array to store the middleware ,use The call order will determine the execution order of the middleware .
Every middleware is a function ( Not a function will report an error ), Receive two parameters , The first is ctx Context object , The other is next function ( from koa-compose Definition )
In the establishment of good http After the server , Would call koa-compose The module of middleware Middleware array for processing . The specific code is not posted here , The principle is : From middleware Take the first function in the array to start execution **, Call in the middleware function next Method will take the next middleware function and continue to execute . Each middleware function will return a promise object .**(ps: call next Method does not mean that the current middleware function has been executed , call next You can continue to execute other code after )
koa The working principle of middleware is similar to the onion model , From the outside to the inside , Inside out ! An example in the code will be given below !
Application level middleware && Error level Middleware
// Application level middleware , The first to perform
app.use(async(ctx,next)=>{
console.log(" I am an application level middleware 1");
// Application level middleware matches any route , If you don't write next() Method , Will not continue to match downward !
await next();
// Error level Middleware , The reason it's written here , It has something to do with the onion model , All application level middleware has been executed , It will be executed from the back to the front await next() Method !
if(ctx.status==404){
ctx.status=404;
ctx.body=' I am a 404 Interface , request was aborted !'
}
})
const Koa = require('koa')
const app = new Koa()
app.use(async (ctx, next) => {
console.log(1)
await next() // What you get here is middleware 2 Back to promise object
console.log(3)
})
app.use((ctx) => {
return new Promise((resolve,reject) => {
setTimeout(() => {
console.log(2)
resolve() // If you don't write here resolve, Then the code will get stuck here !
}, 2000)
})
})
app.listen(3000) // The output is 1 2 3
Routing level middleware
router.get('/news', async (ctx,next) => {
console.log(" This is a news middleware that does not return data !"); // If you don't write next() Method , Will not continue to match the following routing methods !
await next();
})
router.get('/news', async (ctx) => {
ctx.body = " This is the news interface "
})
The last is async and await Use , understand await The following object is promise The difference between objects and ordinary functions !
边栏推荐
猜你喜欢
字节跳动海外技术团队再夺冠:高清视频编码已获17项第一
Activiti directory (V) reject, restart and cancel process
Activit零零碎碎要人命的坑
Shell_ 03_ environment variable
「博士毕业一年,我拿下 ACL Best Paper」
程序员定位解决问题方法论
was unable to send heartbeat
Install docker under windows10 (through Oracle VM VirtualBox)
Mongodb learning notes
字节跳动多篇论文入选 CVPR 2021,精选干货都在这里了
随机推荐
Activiti目录(五)驳回、重新发起、取消流程
Monomer application concept
LeetCode 1637. The widest vertical area between two points without any point
这群程序员中的「广告狂人」,把抖音广告做成了AR游戏
~87 animation
7-12 inventory code base
冯诺依曼体系结构
@RestController、@Controller
控制转移指令
面试集锦库
我走过最迷的路,是字节跳动程序员的脑回路
Typescript basic operations
7-8 likes (need to continue to improve)
MySQL日期函数
Shell_ 06_ Judgment and circulation
Activiti directory (V) reject, restart and cancel process
在 vi 编辑器中的命令模式下,删除当前光标处的字符使用 __ 命 令。
字节跳动春招攻略:学长学姐笔经面经,还有出题人「锦囊」
字节跳动技术面试官现身说法:我最想pick什么样的候选人
Conception du système de thermomètre numérique DS18B20