What is the default timeout for an NPM request module (REST client)?

Next up is my call to node.js to retrieve some data that takes more than 1 minute. Here it will be timeout after 1 minute (60 seconds). I also added a console log for the delay. However, I set the timeout to 120 seconds, but it does not reflect. I know that the default nodejs timeout is 120 seconds by default, but still I get a timeout (60 seconds) from this request module for this call, Please provide information about this.

var options = { method: 'post', url:url, timeout: 120000, json: true, headers: { "Content-Type": "application/json", "X-Authorization": "abc", "Accept-Encoding":"gzip" } } var startTime = new Date(); request(options, function(e, r, body) { var endTime = new Date(); var latencyTime = endTime - startTime; console.log("Ended. latencyTime:"+latencyTime/1000); res.status(200).send(body); }); 
+7
rest npm timeout
source share
1 answer

In request docs parameters , scrolling down to the timeout entry:

timeout Millisecond integer to wait for the server to send the response headers (and start the response body) before interrupting the request. Please note that if the underlying TCP connection cannot be established, the connection timeout throughout the OS will override the timeout parameter (by default, Linux can take from 20 to 120 seconds).

Pay attention to the last part "if the base TCP connection cannot be established, the default connection timeout for the entire system will override the timeout parameter."

There is also a whole Timeouts section. Based on this, and your sample code, we can change the sample request as such

 request(options, function(e, r, body) { if (e.code === 'ETIMEDOUT' && e.connect === true){ // when there a timeout and connect is true, we're meeting the // conditions described for the timeout option where the OS governs console.log('bummer'); } }); 

If so, you will need to decide if a change and acceptability of the OS parameters is possible (this is beyond the scope of this answer, and such a question would be better if the server crashes).

+5
source share

All Articles