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;
})
express/connect var express = require('express') var responseTime = require('response-time') var app = express() app. use(responseTime()) app. get('/', function (req, res) { res. send('hello, world!
However, considering that a “Hello World” Node. js server is easily capable of thirty thousand requests per second on that machine that produced these results, 23 requests per second with an average latency exceeding 3 seconds is dismal.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With