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?
6 Answers
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/');
-
Do you know how often the timeout function runs? Like every second? Every 500 ms? May 18, 2015 at 7:20
-
5
Try this:
var options = {
url: 'http://url',
timeout: 120000
}
request(options, function(err, resp, body) {});
Refer to request's documentation for other options.
-
2I 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.– MarsOnlyMay 29, 2014 at 5:18 -
-
11We should really differentiate between incoming http request and outgoing http request. May 18, 2015 at 7:20
-
1If 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)– JJ StiffApr 22, 2016 at 0:03 -
1Note 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– DeeMar 14, 2019 at 7:57
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);
});
-
1was just going to add this answer myself. was looking for this specifically but didn't see your answer. Jul 29, 2021 at 19:26
-
1Also 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. Feb 7, 2022 at 13:37
-
-
1@MikkoRantalainen thanks for pointing that out. I initially set a higher number to emphasize visibility.– Ivan_ugMar 23 at 10:58
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)
-
1My tests show this to work but I cannot find any documentation for this? Jan 17, 2020 at 13:02
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
-
Hi Rohith. This works perfectly fine in local machine but doesn't work when deployed. Any idea why?– PrajwalJun 25, 2018 at 6:55
-
1How 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?. Jun 25, 2018 at 11:11
-
If 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. Feb 7, 2022 at 13:39
With the latest NodeJS you can experiment with this monkey patch:
const http = require("http");
const originalOnSocket = http.ClientRequest.prototype.onSocket;
require("http").ClientRequest.prototype.onSocket = function(socket) {
const that = this;
socket.setTimeout(this.timeout ? this.timeout : 3000);
socket.on('timeout', function() {
that.abort();
});
originalOnSocket.call(this, socket);
};