How to modify the nodejs request default timeout time?

168,808

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
Share:
168,808
MarsOnly
Author by

MarsOnly

keep charging

Updated on July 05, 2022

Comments

  • MarsOnly
    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
    MarsOnly almost 10 years
    I 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
    Lee almost 10 years
    Could it be server side time-out setting?
  • CMCDragonkai
    CMCDragonkai almost 9 years
    Do you know how often the timeout function runs? Like every second? Every 500 ms?
  • CMCDragonkai
    CMCDragonkai almost 9 years
    We should really differentiate between incoming http request and outgoing http request.
  • a paid nerd
    a paid nerd over 8 years
    Where is this documented?
  • JJ Stiff
    JJ Stiff about 8 years
    If you are using your node app as a proxy, you may want to use both require('request') and require('express'). In such a case, when you make a data via the request module, it will only execute your callback once the request is complete. By specifying the timeout 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
    ilan weissberg over 6 years
  • Prajwal
    Prajwal almost 6 years
    Hi Rohith. This works perfectly fine in local machine but doesn't work when deployed. Any idea why?
  • Rohith K D
    Rohith K D almost 6 years
    How 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
    Dee about 5 years
    Note 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
    Richard Scarrott over 4 years
    My tests show this to work but I cannot find any documentation for this?
  • LizardKing
    LizardKing almost 3 years
    was just going to add this answer myself. was looking for this specifically but didn't see your answer.
  • Mikko Rantalainen
    Mikko Rantalainen about 2 years
    Also 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
    Mikko Rantalainen about 2 years
    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.
  • Naveen Kumar
    Naveen Kumar about 2 years
    Node.js official documentation link for second approach here