Skip to content

Instantly share code, notes, and snippets.

@xmlking
Created October 25, 2015 23:30
Show Gist options
  • Save xmlking/9be31588e1a36d799049 to your computer and use it in GitHub Desktop.
Save xmlking/9be31588e1a36d799049 to your computer and use it in GitHub Desktop.
Koa 2.0.0 supports ES2016 async/await
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