Created
October 25, 2015 23:30
-
-
Save xmlking/9be31588e1a36d799049 to your computer and use it in GitHub Desktop.
Koa 2.0.0 supports ES2016 async/await
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
import Koa from 'koa'; | |
const app = new Koa(); | |
// x-response-time | |
app.use(async (ctx, next) => { | |
var start = new Date; | |
await next(); | |
const ms = new Date - start; | |
ctx.set('X-Response-Time', ms + 'ms'); | |
}); | |
// logger | |
app.use(async (ctx, next) => { | |
const start = new Date; | |
await next(); | |
const ms = new Date - start; | |
console.log(`${ctx.method} ${ctx.url} - ${ms}`); | |
}); | |
// response | |
app.use( ctx => { | |
ctx.body = 'Hello World'; | |
}); | |
app.listen(3000); | |
console.log('Listening on http://localhost:3000'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment