Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. import koa from 'koa';
  2. const app:koa = new koa();
  3.  
  4. // logger
  5. app.use(async (ctx:koa.Context, next:Function):Promise<any> => {
  6. await next();
  7. const rt = ctx.response.get('X-Response-Time');
  8. console.log(`${ctx.method} ${ctx.url} - ${rt}`);
  9. });
  10.  
  11. // x-response-time
  12. app.use(async (ctx:koa.Context, next:Function):Promise<any> => {
  13. const start = Date.now();
  14. await next();
  15. const ms = Date.now() - start;
  16. ctx.set('X-Response-Time', `${ms}ms`);
  17. });
  18.  
  19. // response
  20. app.use(async (ctx:koa.Context) => {
  21. ctx.body = 'Hello World';
  22. });
  23.  
  24. app.listen(3000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement