Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js request web page

Tags:

http

node.js

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?

like image 303
James Avatar asked Apr 05 '13 16:04

James


1 Answers

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)
})
like image 173
Noah Avatar answered Oct 14 '22 17:10

Noah