Node.js request web page

I need to connect to a web page and return the status code of the page, which I've been able to achieve using http.request however the pages I need to request can take a long time, sometimes several minutes, so I'm always getting a socket hang up error.

I'm using the following code so far:

var reqPage = function(urlString, cb) {
    // Resolve the URL
    var path = url.parse(urlString);
    var req = http.request({
        host: path.hostname,
        path: path.pathname,
        port: 80,
        method: 'GET'
    });
    req.on('end', function() {
        cb.call(this, res);
    });
    req.on('error', function(e) {
        winston.error(e.message);
    });
};

What do I need to do to ensure that my application still attempts to connect to the page even if it's going to take a few minutes?

Use the request module and set the timeout option to an appropriate value (in milliseconds)

var request = require('request')
var url = 'http://www.google.com' // input your url here

// use a timeout value of 10 seconds
var timeoutInMilliseconds = 10*1000
var opts = {
  url: url,
  timeout: timeoutInMilliseconds
}

request(opts, function (err, res, body) {
  if (err) {
    console.dir(err)
    return
  }
  var statusCode = res.statusCode
  console.log('status code: ' + statusCode)
})

Add this if you don't want to use a higher level http client like request or superagent , then add this...

req.on("connection", function(socket){
    socket.setTimeout((1000*60*5)); //5 mins  
});