Node.js. Get the time of the HTTP request
I am a .NET developer and new to Node.js so I would be very grateful for your understanding and help.
I have some code that logs the time of an HTTP request:
var sTime;
var httpReq = http.request(requestOptions, function (httpRes)
{
httpRes.on('end', function ()
{
var diffTime = (sTime) ? new String(new Date() - sTime) : '';
log('diffTime: ' + diffTime);
});
});
httpReq.on('socket', function (reqSocket)
{
sTime = new Date();
});
I sometimes get diffTime: 0
in the log file which is not possible. I can try another way of logging the request time, for example, but I want to find the reason for the zero time.
+3
source to share
1 answer
Without the rest of your actual code, it's a little difficult for you to verify your problem. Here's a similar method that will display the time difference.
var request = require("request");
setInterval(function(){
doRequest();
}, 200);
function doRequest() {
var startTime, diffTime;
//Start the timer.
startTime = new Date();
request("http://www.google.com", function(err,res,body){
diffTime = ( new Date() - startTime );
console.log(diffTime);
});
};
EDIT - see explanation here http://nodeio.ninja/http/measuring-http-request-times-in-nodejs/
0
source to share