diff --git a/packages/server/src/app.ts b/packages/server/src/app.ts index 83aa4f5cfc..3e6180d1f8 100644 --- a/packages/server/src/app.ts +++ b/packages/server/src/app.ts @@ -84,6 +84,14 @@ async function main() { const app = new Koa(); + // app.use(async function responseTime(ctx:AppContext, next:Function) { + // const start = Date.now(); + // await next(); + // const ms = Date.now() - start; + // console.info('Response time', ms) + // //ctx.set('X-Response-Time', `${ms}ms`); + // }); + // Note: the order of middlewares is important. For example, ownerHandler // loads the user, which is then used by notificationHandler. And finally // routeHandler uses data from both previous middlewares. It would be good to diff --git a/packages/server/src/middleware/routeHandler.ts b/packages/server/src/middleware/routeHandler.ts index 4b7d836587..98d974b37b 100644 --- a/packages/server/src/middleware/routeHandler.ts +++ b/packages/server/src/middleware/routeHandler.ts @@ -3,7 +3,7 @@ import { AppContext, Env } from '../utils/types'; import { isView, View } from '../services/MustacheService'; export default async function(ctx: AppContext) { - ctx.appLogger().info(`${ctx.request.method} ${ctx.path}`); + const requestStartTime = Date.now(); try { const responseObject = await execRequest(ctx.routes, ctx); @@ -56,5 +56,10 @@ export default async function(ctx: AppContext) { if (error.code) r.code = error.code; ctx.response.body = r; } + } finally { + // Technically this is not the total request duration because there are + // other middlewares but that should give a good approximation + const requestDuration = Date.now() - requestStartTime; + ctx.appLogger().info(`${ctx.request.method} ${ctx.path} (${requestDuration}ms)`); } }