示例 const Koa = require('koa'); const router = require('koa-router')(); const app = new Koa(); app.use(async (ctx, next) = { console.log('01'); next(); console.log('001'); }) app.use(async (ctx, next) = { console.log('02'); await next();
示例
const Koa = require('koa');const router = require('koa-router')();
const app = new Koa();
app.use(async (ctx, next) => {
console.log('01');
next();
console.log('001');
})
app.use(async (ctx, next) => {
console.log('02');
await next();
console.log('002');
})
// 配置路由
router
.get('/', async (ctx, next) => {
console.log('03');
next();
console.log('003');
})
.get('/', async (ctx, next) => {
console.log('04');
ctx.body = 'Hello';
})
app
.use(router.routes())
.use(router.allowedMethods())
.listen(3000);
console.log('Server run in http://localhost:3000/');
执行顺序
- http://localhost:3000/ 控制台输出
- 01 - 02 - 03 - 04 - 003 - 002 - 001
- 形象图: