SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 77 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top