Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import koa from 'koa';
- const app:koa = new koa();
- // logger
- app.use(async (ctx:koa.Context, next:Function):Promise<any> => {
- await next();
- const rt = ctx.response.get('X-Response-Time');
- console.log(`${ctx.method} ${ctx.url} - ${rt}`);
- });
- // x-response-time
- app.use(async (ctx:koa.Context, next:Function):Promise<any> => {
- const start = Date.now();
- await next();
- const ms = Date.now() - start;
- ctx.set('X-Response-Time', `${ms}ms`);
- });
- // response
- app.use(async (ctx:koa.Context) => {
- ctx.body = 'Hello World';
- });
- app.listen(3000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement