node.js Express.js 响应超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21708208/
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
Express.js Response Timeout
提问by Xerri
PROBLEM
问题
I've been looking for request/response timeouts for Express.js but everything seems to be related to the connection rather than the request/response itself.
我一直在寻找 Express.js 的请求/响应超时,但一切似乎都与连接有关,而不是请求/响应本身。
If a request is taking a long time, it should be timed out. Obviously this shouldn't happen but even a simple mistake as having a route handler without a call to the callback or without res.send(), the browser will keep waiting for a reply forever.
如果一个请求需要很长时间,它应该超时。显然这不应该发生,但即使是一个简单的错误,例如没有调用回调或没有调用的路由处理程序res.send(),浏览器将永远等待回复。
An empty route handler is a perfect example of this.
空路由处理程序就是一个完美的例子。
app.get('/sessions/', function(req, res, callback){});
FIX
使固定
I added the following beforeapp.use(app,router);and it seemed to add the timeout functionality. Does anyone have any experience/opinion on this?
我之前添加了以下内容app.use(app,router);,它似乎添加了超时功能。有没有人对此有任何经验/意见?
app.use(function(req, res, next){
res.setTimeout(120000, function(){
console.log('Request has timed out.');
res.send(408);
});
next();
});
Note that I've set the timeout to 2 minutes.
请注意,我已将超时设置为 2 分钟。
回答by srquinn
There is already a Connect Middleware for Timeout support:
已经有一个用于超时支持的连接中间件:
var timeout = express.timeout // express v3 and below
var timeout = require('connect-timeout'); //express v4
app.use(timeout(120000));
app.use(haltOnTimedout);
function haltOnTimedout(req, res, next){
if (!req.timedout) next();
}
If you plan on using the Timeout middleware as a top-level middleware like above, the haltOnTimedOutmiddleware needs to be the last middleware defined in the stack and is used for catching the timeout event. Thanks @Aichholzer for the update.
如果您打算使用 Timeout 中间件作为上面的顶级中间件,则该haltOnTimedOut中间件需要是堆栈中定义的最后一个中间件,用于捕获超时事件。感谢@Aichholzer 的更新。
Side Note:
边注:
Keep in mind that if you roll your own timeout middleware, 4xx status codes are for client errors and 5xx are for server errors. 408s are reserved for when:
请记住,如果您推出自己的超时中间件,4xx 状态代码用于客户端错误,5xx 用于服务器错误。408s 保留用于以下情况:
The client did not produce a request within the time that the server was prepared to wait. The client MAY repeat the request without modifications at any later time.
客户端没有在服务器准备等待的时间内产生请求。客户端可以在以后的任何时间不加修改地重复请求。
回答by V31
An update if one is using Express 4.2 then the timeout middleware has been removed so need to manually add it with
如果使用 Express 4.2 的更新,则超时中间件已被删除,因此需要手动添加它
npm install connect-timeout
and in the code it has to be (Edited as per comment, how to include it in the code)
并且在代码中它必须是(根据评论编辑,如何将其包含在代码中)
var timeout = require('connect-timeout');
app.use(timeout('100s'));
回答by bereket gebredingle
You don't need other npm modules to do this
您不需要其他 npm 模块来执行此操作
var server = app.listen();
server.setTimeout(500000);
inspired by https://github.com/expressjs/express/issues/3330
灵感来自https://github.com/expressjs/express/issues/3330
or
或者
app.use(function(req, res, next){
res.setTimeout(500000, function(){
// call back function is called when request timed out.
});
next();
});
回答by cider
In case you would like to use timeout middleware and exclude a specific route:
如果您想使用超时中间件并排除特定路由:
var timeout = require('connect-timeout');
app.use(timeout('5s')); //set 5s timeout for all requests
app.use('/my_route', function(req, res, next) {
req.clearTimeout(); // clear request timeout
req.setTimeout(20000); //set a 20s timeout for this request
next();
}).get('/my_route', function(req, res) {
//do something that takes a long time
});
回答by rahul shukla
request.setTimeout(< time in milliseconds >)does the job
request.setTimeout(< time in milliseconds >)做这份工作
https://nodejs.org/api/http.html#http_request_settimeout_timeout_callback
https://nodejs.org/api/http.html#http_request_settimeout_timeout_callback
回答by destino
Before you set your routes, add the code:
在设置路由之前,添加代码:
app.all('*', function(req, res, next) {
setTimeout(function() {
next();
}, 120000); // 120 seconds
});

