How to change nodejs request timeout timeout?

I am using a Node / express server. The default express timeout is 120,000 ms, but this is not enough for me. When my answer reaches 120,000 ms, the console will go into the POST /additem 200 120006ms and an error message will appear on the page, so I want to set the timeout to a larger value. How should I do it?

+59
timeout express request
May 29 '14 at 3:33
source share
4 answers

I assume you are using express , given the logs you have in your question. The key is to set the timeout property on the 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 do not use express and work only with vanilla node, the principle is the same. The following data is not returned:

 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/'); 
+84
May 29 '14 at 6:19 06:19
source share

Try the following:

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

Refer to the request documentation for other parameters.

+37
May 29 '14 at 3:37
source share

For a specific request, you can set timeOut to 0, which is not a timeout, until we get a response from the database or another server

 request.setTimeout(0) 
+9
Aug 28 '17 at 6:58
source share

For those with a configuration in bin/www , just add the timeout parameter after creating the http server.

 var server = http.createServer(app); /** * Listen on provided port, on all network interfaces */ server.listen(port); server.timeout=yourValueInMillisecond 
+4
Sep 22 '17 at 11:00
source share



All Articles