Skip to content

Instantly share code, notes, and snippets.

@nathantowell
Created August 2, 2017 12:13
Show Gist options
  • Save nathantowell/1e14cb49d1dc076808de7f80eba3e89d to your computer and use it in GitHub Desktop.
Save nathantowell/1e14cb49d1dc076808de7f80eba3e89d to your computer and use it in GitHub Desktop.
Koa hello world example with response time middleware
'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