How to modify the nodejs request default timeout time?
Solution 1
I'm assuming you're using express
, given the logs you have in your question. The key is to set the timeout
property on server (the following sets the timeout to one second, use whatever value you want):
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
server.timeout = 1000;
If you're not using express and are only working with vanilla node, the principle is the same. The following will not return data:
var http = require('http');
var server = http.createServer(function (req, res) {
setTimeout(function() {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}, 200);
}).listen(1337, '127.0.0.1');
server.timeout = 20;
console.log('Server running at http://127.0.0.1:1337/');
Solution 2
Try this:
var options = {
url: 'http://url',
timeout: 120000
}
request(options, function(err, resp, body) {});
Refer to request's documentation for other options.
Solution 3
Linking to express issue #3330
You may set the timeout either globally for entire server:
var server = app.listen();
server.setTimeout(500000);
or just for specific route:
app.post('/xxx', function (req, res) {
req.setTimeout(500000);
});
Solution 4
For specific request one can set timeOut to 0 which is no timeout till we get reply from DB or other server
request.setTimeout(0)
Solution 5
For those having configuration in bin/www
, just add the timeout parameter after http server creation.
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces
*/
server.listen(port);
server.timeout=yourValueInMillisecond
Comments
-
MarsOnly almost 2 years
I'm using a Node/express server. The default timeout of express is 120,000 ms, but it is not enough for me. When my response reaches 120,000 ms, the console will log
POST /additem 200 120006ms
and the page shows an error, so I want to set the timeout to a larger value. How would I do that? -
MarsOnly almost 10 yearsI have tried this,but it doesn't work.when the response reaches 120000 ms, the console still display
POST /additem 200 120006ms
and the page shows error. -
Lee almost 10 yearsCould it be server side time-out setting?
-
CMCDragonkai almost 9 yearsDo you know how often the timeout function runs? Like every second? Every 500 ms?
-
CMCDragonkai almost 9 yearsWe should really differentiate between incoming http request and outgoing http request.
-
a paid nerd over 8 yearsWhere is this documented?
-
JJ Stiff about 8 yearsIf you are using your node app as a proxy, you may want to use both
require('request')
andrequire('express')
. In such a case, when you make a data via therequest
module, it will only execute your callback once the request is complete. By specifying thetimeout
as @Lee is suggesting, you are 'failing' the request and calling the callback before data is returned. For me, this is often better than failing via express, as @SomeKittens suggests, because I often require cleanup from my request before exiting the process. I would suggest using 2000 (2 sec) rather than 120000 (2 mins) -
ilan weissberg over 6 yearstimeout it's a valid param github.com/request/request#user-content-requestoptions-callback
-
Prajwal almost 6 yearsHi Rohith. This works perfectly fine in local machine but doesn't work when deployed. Any idea why?
-
Rohith K D almost 6 yearsHow are you running it in deployment? Is it using PM2, forever?. Is bin/www used for HTTP server configuration when deployed or from any other source?.
-
Dee about 5 yearsNote that if the underlying TCP connection cannot be established, the OS-wide TCP connection timeout will overrule the timeout option (the default in Linux can be anywhere from 20-120 seconds). npmjs.com/package/request#requestoptions-callback
-
Richard Scarrott over 4 yearsMy tests show this to work but I cannot find any documentation for this?
-
LizardKing almost 3 yearswas just going to add this answer myself. was looking for this specifically but didn't see your answer.
-
Mikko Rantalainen about 2 yearsAlso note that the response has another timeout which you might want to set, too. Consider slowaris attack before blindly setting it to very high number, though.
-
Mikko Rantalainen about 2 yearsIf behavior changes when running on actual server, you may be having flakey connection (TCP/IP connection drops randomly) or you have some kind of (more or less transparent) reverse proxy in between causing the timeout.
-
Naveen Kumar about 2 yearsNode.js official documentation link for second approach here