Skip to content

Instantly share code, notes, and snippets.

@ryanlid
Created August 12, 2019 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanlid/2280b9f9a49a8248ecda570384dc56f9 to your computer and use it in GitHub Desktop.
Save ryanlid/2280b9f9a49a8248ecda570384dc56f9 to your computer and use it in GitHub Desktop.
使用中间件获取响应时间
const koa = require("koa");
const app = new koa();
app.use(async (ctx, next) => {
let stime = new Date().getTime();
await next();
let etime = new Date().getTime();
ctx.response.type = "text/html";
ctx.response.body = "<h1>Hello World</h1>";
console.log(`请求地址:${ctx.path},响应时间:${etime - stime}`);
});
app.use(async (ctx, next) => {
console.log("中间件 doSomething");
await next();
console.log("中间件执行 over");
});
app.listen(3000, () => {
console.log("server is running at http://localhost:3000");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment