Created
August 12, 2019 10:07
-
-
Save ryanlid/2280b9f9a49a8248ecda570384dc56f9 to your computer and use it in GitHub Desktop.
使用中间件获取响应时间
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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