nodejs request library, get the response time

node.js

Solution

The request library can do timing for you (docs):

request.get({
  url : 'http://example.com',
  time : true
},function(err, response){
  console.log('Request time in ms', response.elapsedTime);
});

As the question implies, you could get issues with the approach of starting a timer, calling `request` then stopping the timer in the callback:

var start = new Date();
request.get('http://example.com', function(err, response){
  // NOT GOOD
  console.log('Request time plus 5 seconds', new Date() - start);
});
require('sleep').sleep(5); // time-consuming synchronous action

Problem

Using the nodejs `request` library: https://github.com/mikeal/request ``` var request = require('request'); request('http://example.com', function (error, response, body) { ... }) ``` Is it possible to get the response time on the callback? The doc mentions response.statusCode. looking at the library source code I also see undocumented response.headers and response.href, but I don't see responseTime or similar. or, is there an alternative library to `request` that provides the response time? ps: I know I could do something like this, but that's not a solution, as I am making many async requests and I cannot control when each request will be started. ``` var request = require('request'); var start = new Date(); request('http://example.com', function (error, response, body) { ... var responseTime = new Date() - start; }) ```

Original source