First, onion model
In simple terms, there are layers of processing between the request and the response that are performed by the middleware
2. Koa middleware
1. Comparison between Express and Koa
-
Take a look at Express (the last generation of Koa), which starts with 4.x and is integrated by defaultThree middleware.
-
Take a look at the Koa introduction, in fact, Koa does not bundle any middleware, equivalent to the green version of Express
2. Koa middleware selection principles
Koa’s green model has its pros and cons,The advantage is that there is no dependence, refreshing; The downside is that building a production-ready shelf is more cumbersome and tests the developer's tripartite selection ability
Generally, the selection of third-party middleware is based on the following points:
1. Whether functions can cover requirements
Whether the functionality provided by the middleware can cover your requirements is not one of the core conditions
2. Community activity
Generally, the more active the community, the more attention, the more bug fixes, and the more frequent releases
3. Check whether the documents and Demo are complete
Usually 2 more active, this score will not be low
4. Middleware volume
If 1, 2, 3 are the same, let’s think about the volume
Third, actual combat drills
1. Write a middleware
We actually used middleware in the first installment of this article, as follows:
const koa=require('koa');
const app=new koa();
// This is also middleware, but the middleware terminates the request
app.use( async (ctx)=>{
ctx.body='Hello Koa world'
})
app.listen(3000.() = >{
console.log('Startup success! http://localhost:3000')});Copy the code
In order to better reflect the Onion model, we add another middleware with next before this.
const koa=require('koa');
const app=new koa();
// Front middleware, with next delivery
app.use( async (ctx, next)=>{
console.log('I am the front middleware')
await next()
})
// Configure the middleware
app.use( async (ctx)=>{
ctx.body='Hello Koa world'
})
app.listen(3000.() = >{
console.log('Startup success! http://localhost:3000')});Copy the code
Request:
Background output:
2. Add more ingredients
So in that example, we had the left side of the onion, what about the right side? Where did it go?
Let’s change the example as follows
const koa=require('koa');
const app=new koa();
// Front middleware
app.use( async (ctx, next)=>{
console.log('I'm pre-middleware -request') // <<=== add
await next()
console.log('I am front middleware -response') // <<=== add
})
app.use( async (ctx)=>{
console.log('Hello') // <<=== add
ctx.body='Hello Koa world'
})
app.listen(3000.() = >{
console.log('Startup success! http://localhost:3000')});Copy the code
View the running result:
Multiple pieces of middleware form a middle stack and execute in a first-in-last-out order.
- The outermost middleware executes first.
- call
next
Function, passing execution to the next middleware.- .
- The innermost layer of middleware executes last.
- After execution, the execution is handed back to the middleware of the previous layer.
- .
- After the outermost middleware takes back the right to execute, execute
next
The code after the function.Quoted from ruan Yifeng teacher’s blog