I am a bit confused regarding the response time token that is printed from morgan middleware with nodejs. Via postman i see reponse time say 100+ms, but in the logs using the middleware, the response time is like 0.1 and around that. At a first glance this may look like i have divided by thousand to get the seconds value but i havent.
This is my code:
const morganMiddleware = morgan(
(tokens, req, res) => JSON.stringify({
method: tokens.method(req, res),
url: tokens.url(req, res),
status: Number.parseFloat(tokens.status(req, res)),
content_length: tokens.res(req, res, 'content-length'),
response_time: Number.parseFloat(tokens['response-time'](req, res)),
}),
{
stream: {
// Configure Morgan to use our custom logger with the http severity
write: (message) => {
const data = JSON.parse(message);
logger.http('incoming-request', data);
},
},
},
);
I am expecting the response time to be the same in postman and in the logger. If not exactly then atleast in the vicinity
When you see a difference in response times between Postman and Morgan, it's probably because they measure slightly different things. Postman's timer starts when you hit "send" and stops when it gets the full response, accounting for all the network travel time.
Morgan, in your setup, is only timing how long your server takes to process the request and send a response, so it doesn't include any of that network travel time. Plus Morgan's response time is in seconds with millisecond precision.
So, if you see 0.123 in Morgan, that's actually 123 milliseconds. To get it in a similar format as Postman, just adjust your logging to multiply by 1000
Number.parseFloat(tokens['response-time'](req, res)) * 1000This should give you a value closer to what you're seeing in Postman, though there might always be a bit of a difference because of the network.