当前位置:网站首页>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 !
边栏推荐
- ~75 background
- Solr new core
- MySQL字符串函数
- Which is more important for programming, practice or theory [there are some things recently, I don't have time to write an article, so I'll post an article on hydrology, and I'll fill in later]
- 7-12 inventory code base
- Activiti directory (III) deployment process and initiation process
- 数据传送指令
- 汇编语言基础知识
- Fdog series (III): use Tencent cloud SMS interface to send SMS, write database, deploy to server, web finale.
- Conception du système de thermomètre numérique DS18B20
猜你喜欢

~84 form supplement

字节跳动多篇论文入选 CVPR 2021,精选干货都在这里了

Usage of insert() in vector

~79 Movie card exercise

Idea resolving jar package conflicts

Erlang installation

Fdog series (I): think about it. It's better to write a chat software. Then start with the imitation QQ registration page.

~85 transition

程序员定位解决问题方法论

The QT program compiled on CentOS lacks a MySQL driven solution
随机推荐
Eureka high availability
Idea resolving jar package conflicts
Restful style interface design
DOS 功能调用
Solr standalone installation
GCC error: terminate called after throwing an instance of 'std:: regex_ error‘ what(): regex
Activiti目录(四)查询代办/已办、审核
一个数10年工作经验的微服务架构老师的简历
100张图训练1小时,照片风格随意变,文末有Demo试玩|SIGGRAPH 2021
Alibaba cloud server builds SVN version Library
~87 animation
这群程序员中的「广告狂人」,把抖音广告做成了AR游戏
面试集锦库
Many papers on ByteDance have been selected into CVPR 2021, and the selected dry goods are here
Typescript basic operations
Activiti directory (III) deployment process and initiation process
登陆验证koa-passport中间件的简单使用
~84 form supplement
Fdog series (III): use Tencent cloud SMS interface to send SMS, write database, deploy to server, web finale.
唯有學C不負眾望 TOP5 S1E8|S1E9:字符和字符串&&算術運算符