node.js - Setting socket based timeout on http request -


the code below aborts request if takes longer 10 seconds duration.

is possible set socket based timeout on request using http module?

also, should using socket , request timeout?

var abortrequesterror; var timeout = 10000; var requestobject = {   host: 'xx.xx.xx.xx',   path: '/api',   port: 10000,   method: 'get' };  var req = http.request(requestobject, function(res) {    var responsebody = '';   res.on('data', function(data) {     responsebody += data;   });    res.on('end', function() {     console.log(abortrequesterror);   });  }).on('error', function(error) {    error = abortrequesterror ? abortrequesterror : error;   console.log(error);  }).settimeout(timeout, function() {    abortrequesterror = new error('request timed out');   req.abort(); }); 

for connection timeouts, node uses os defaults. simplified, these triggered when can't reach server @ within period of time.

after established connection, request#settimeout() function sets idle timeout, triggers when there have been no network activity period of time. sounds want. (note underneath calls socket#settimeout when socket created, there no need call both.)

for other kinds of logic, you'll need set own thing manual usage of timers. code provided should work fine need — if, after being connected to, server stops sending data longer 10,000 milliseconds, timeout trigger.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -