const app = new Koa();
app.use((ctx, next) => {
console.log(1);
next();
console.log(2);
});
app.use((ctx, next) => {
console.log(3);
next();
console.log(4);
});
app.listen(8000, '0.0.0.0', () => {
console.log(`Server is starting`);
});
// 输出
Server is starting
1
3
4
2
- 每一层相当于一个中间件,用来处理特定的功能
- 先执行next()前请求,再执行next()函数,最后执行next()后响应
- 利用洋葱模型可以计算操作耗时