http - Node.js request web page -


i need connect web page , return status code of page, i've been able achieve using http.request pages need request can take long time, several minutes, i'm getting socket hang up error.

i'm using following code far:

var reqpage = function(urlstring, cb) {     // resolve 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 need ensure application still attempts connect page if it's going take few minutes?

use request module , set timeout option appropriate value (in milliseconds)

var request = require('request') var url = 'http://www.google.com' // input url here  // use 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) }) 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -