How to modify the nodejs request default timeout time? How to modify the nodejs request default timeout time? express express

How to modify the nodejs request default timeout time?


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/');


Try this:

var options = {    url:  'http://url',    timeout: 120000}request(options, function(err, resp, body) {});

Refer to request's documentation for other options.


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)