Last active
November 10, 2018 15:52
-
-
Save micheleriva/d6efbdd574ea422017c81f3f71391ed7 to your computer and use it in GitHub Desktop.
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
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