Node.js request web page

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() {, res);
    req.on('error', function(e) {

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?

Problem courtesy of: James


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

var request = require('request')
var url = '' // 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) {
  var statusCode = res.statusCode
  console.log('status code: ' + statusCode)
Solution courtesy of: Noah


Leave a Reply

Your email address will not be published. Required fields are marked *