如何修改nodejs请求默认超时时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23925284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to modify the nodejs request default timeout time?
提问by MarsOnly
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 120006msand the page shows an error, so I want to set the timeout to a larger value. How would I do that?
我正在使用 Node/express 服务器。express 的默认超时时间是 120,000 毫秒,但对我来说还不够。当我的响应达到120,000 ms时,控制台会记录POST /additem 200 120006ms并且页面显示错误,因此我想将超时设置为更大的值。我该怎么做?
回答by SomeKittens
I'm assuming you're using express, given the logs you have in your question. The key is to set the timeoutproperty on server (the following sets the timeout to one second, use whatever value you want):
express鉴于您在问题中的日志,我假设您正在使用。关键是timeout在服务器上设置属性(以下将超时设置为一秒,使用您想要的任何值):
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:
如果你不使用 express 而只使用 vanilla node,原理是一样的。以下不会返回数据:
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/');
回答by Lee
回答by Bhavik
For specific request one can set timeOut to 0 which is no timeout till we get reply from DB or other server
对于特定的请求,可以将 timeOut 设置为 0,这在我们从 DB 或其他服务器得到回复之前不会超时
request.setTimeout(0)
回答by Rohith K D
For those having configuration in bin/www, just add the timeout parameter after http server creation.
对于那些在 中配置的bin/www,只需在 http 服务器创建后添加超时参数。
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces
*/
server.listen(port);
server.timeout=yourValueInMillisecond
回答by Ivan_ug
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);
});
回答by Bela Vizy
With the latest NodeJS you can experiment with this monkey patch:
使用最新的 NodeJS,您可以试验这个猴子补丁:
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);
};

