Express.js HTTP请求超时
我想知道是否有人能告诉我使用express时默认的HTTP请求超时是多少 我的意思是:当浏览器或服务器手动关闭连接时,Express/Node.js服务器在处理http请求多少秒后会关闭连接 如何更改单个路由的超时?我想设置为15分钟左右的特殊音频转换路线 非常感谢Express.js HTTP请求超时,http,node.js,httprequest,express,Http,Node.js,Httprequest,Express,我想知道是否有人能告诉我使用express时默认的HTTP请求超时是多少 我的意思是:当浏览器或服务器手动关闭连接时,Express/Node.js服务器在处理http请求多少秒后会关闭连接 如何更改单个路由的超时?我想设置为15分钟左右的特殊音频转换路线 非常感谢 Tom请求连接设置超时(毫秒)显示在Node.js中设置HTTP服务器的请求超时。req.connection.setTimeout(毫秒)可能是个坏主意,因为可以通过同一套接字发送多个请求 尝试或使用以下方法: var error
Tom请求连接设置超时(毫秒)显示在Node.js中设置HTTP服务器的请求超时。
req.connection.setTimeout(毫秒)代码>可能是个坏主意,因为可以通过同一套接字发送多个请求
尝试或使用以下方法:
var errors = require('./errors');
const DEFAULT_TIMEOUT = 10000;
const DEFAULT_UPLOAD_TIMEOUT = 2 * 60 * 1000;
/*
Throws an error after the specified request timeout elapses.
Options include:
- timeout
- uploadTimeout
- errorPrototype (the type of Error to throw)
*/
module.exports = function(options) {
//Set options
options = options || {};
if(options.timeout == null)
options.timeout = DEFAULT_TIMEOUT;
if(options.uploadTimeout == null)
options.uploadTimeout = DEFAULT_UPLOAD_TIMEOUT;
return function(req, res, next) {
//timeout is the timeout timeout for this request
var tid, timeout = req.is('multipart/form-data') ? options.uploadTimeout : options.timeout;
//Add setTimeout and clearTimeout functions
req.setTimeout = function(newTimeout) {
if(newTimeout != null)
timeout = newTimeout; //Reset the timeout for this request
req.clearTimeout();
tid = setTimeout(function() {
if(options.throwError && !res.finished)
{
//throw the error
var proto = options.error == null ? Error : options.error;
next(new proto("Timeout " + req.method + " " + req.url) );
}
}, timeout);
};
req.clearTimeout = function() {
clearTimeout(tid);
};
req.getTimeout = function() {
return timeout;
};
//proxy end to clear the timeout
var oldEnd = res.end;
res.end = function() {
req.clearTimeout();
res.end = oldEnd;
return res.end.apply(res, arguments);
}
//start the timer
req.setTimeout();
next();
};
}
中的默认请求超时。这就是express的用途
您可以使用以下方法增加单个管线的流量:
app.get('/longendpoint', function (req, res) {
req.setTimeout(360000); // 5 minutes
...
});
这将设置连接超时,而不是请求超时。