Skip to content

Instantly share code, notes, and snippets.

@micheleriva
Last active November 10, 2018 15:52
Show Gist options
  • Save micheleriva/d6efbdd574ea422017c81f3f71391ed7 to your computer and use it in GitHub Desktop.
Save micheleriva/d6efbdd574ea422017c81f3f71391ed7 to your computer and use it in GitHub Desktop.
const Koa = require('koa')
const Router = require('koa-router')
const runJob = require('./modules/job')
const log = require('./modules/log')
const router = new Router()
const app = new Koa()
router.get('/', async ctx => ctx.body = `PID ${process.pid} listening here!`)
.post('/flip', async ctx => {
const res = await runJob()
ctx.body = res
})
app.use(async (ctx, next) => {
await next();
const rt = ctx.response.get('X-Response-Time');
log(`${ctx.method} ${ctx.url} - ${rt}`);
})
.use(async (ctx, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
ctx.set('X-Response-Time', `${ms}ms`);
})
.use(router.routes())
.listen(3000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment