// 演示koa2 中间件的洋葱圈模型
const Koa = require('koa');
const app = new Koa;// logger ctx = req+res
app.use(async(ctx, next) => {
await next(); // 执行下一个中间件
const rt = ctx.response.get('X-Response-Time') // res获取时间差
console.log(`${ctx.method} ${ctx.url} - ${rt}`)
})// x-response-time
app.use(async(ctx, next) => {
const start = Date.now();
await next(); // 执行下一个中间件
const ms = Date.now()- start; // 计算时间差
ctx.set('X-Response-Time', `${ms}ms`) // 记录/设置时间差
})// response
app.use(async(ctx, next) => {
ctx.body = 'Hello world';
})
app.listen(3000);
console.log('koa2已经开始监听3000端口')
本文链接:https://blog.csdn.net/u014627255/article/details/122906575