Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify the nodejs request default timeout time?

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?

like image 332
MarsOnly Avatar asked May 29 '14 03:05

MarsOnly


People also ask

How do I set HTTP request timeout?

Timeouts on http. request() takes a timeout option. Its documentation says: timeout <number> : A number specifying the socket timeout in milliseconds. This will set the timeout before the socket is connected.

What is the default timeout for HTTP request?

The default value is 60 seconds. If the value of this stanza entry is set to 0 (or not set), connection timeouts between data fragments are governed instead by the client-connect-timeout stanza entry. The exception to this rule occurs for responses returned over HTTP (TCP).


2 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/'); 
like image 162
SomeKittens Avatar answered Sep 21 '22 03:09

SomeKittens


Try this:

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

Refer to request's documentation for other options.

like image 38
Lee Avatar answered Sep 22 '22 03:09

Lee