Created
August 2, 2017 12:13
-
-
Save nathantowell/1e14cb49d1dc076808de7f80eba3e89d to your computer and use it in GitHub Desktop.
Koa hello world example with response time middleware
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
'use strict' | |
const Koa = require('koa') | |
const app = new Koa() | |
app.use(async (context, next) => { | |
let start = Date.now() | |
await next() | |
let ms = Date.now() - start | |
context.set('X-Response-Time', ms + 'ms') | |
console.log(context.method + ' ' + context.url + ' - ' + ms + 'ms') | |
}) | |
app.use(async context => { | |
context.body = 'Hello, World!' | |
}) | |
app.listen(3000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment