docs: better demonstrate middleware flow (#1195)

This commit is contained in:
Jason Macgowan 2018-06-06 02:52:20 -04:00 committed by Yiyu He
parent ee1a933096
commit 4d42500e76

View file

@ -78,6 +78,14 @@ app.listen(3000);
const Koa = require('koa');
const app = new Koa();
// logger
app.use(async (ctx, next) => {
await next();
const rt = ctx.response.get('X-Response-Time');
console.log(`${ctx.method} ${ctx.url} - ${rt}`);
});
// x-response-time
app.use(async (ctx, next) => {
@ -87,15 +95,6 @@ app.use(async (ctx, next) => {
ctx.set('X-Response-Time', `${ms}ms`);
});
// logger
app.use(async (ctx, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
console.log(`${ctx.method} ${ctx.url} - ${ms}`);
});
// response
app.use(async ctx => {