response-time icon indicating copy to clipboard operation
response-time copied to clipboard

Injecting response times into view templates

Open kevinSuttle opened this issue 10 years ago • 5 comments

Reminder: https://stackoverflow.com/questions/30019235/how-to-access-all-response-times-on-every-route-in-express-4/30029024#30029024

Thanks @dougwilson.

kevinSuttle avatar May 05 '15 05:05 kevinSuttle

So you cannot access the response time until you've actually responded. I can add an API so you can access the "total elapsed time since the request arrived" which will be sort of like a response time. Would that work?

dougwilson avatar May 05 '15 15:05 dougwilson

I'm not sure I would know until I saw it in action. Maybe I should leverage https://developer.mozilla.org/en-US/docs/Navigation_timing on the client instead, but I don't know if that's battling the same race condition.

kevinSuttle avatar May 05 '15 15:05 kevinSuttle

We could have this module attach a function to the req object or something. I'm just trying to brainstorm it out with you :) like maybe req.elapsedTime() or something?

It may also be useful to know that since this module normally does response time, and response time is not actually available to your code until you actually... respond, then there is a work-around with the existing code: build all your response headers and explicitly res.writeHead() to respond with the headers, then build your view at which point res.getHeader will have the response time, of course, since you've already started to respond.

dougwilson avatar May 05 '15 15:05 dougwilson

Yeah, we can try req.elapsedTime.

kevinSuttle avatar May 05 '15 16:05 kevinSuttle

Hi @berkantipek yes, that won't work, which is why this issue is open. The function is called after your view has already been rendered (because rendering the view takes time, which contributes to the response time... you don't know the response time until after you have performed all actions involved in responding, including rendering your view).

dougwilson avatar Jul 15 '17 19:07 dougwilson