Express.js HTTP 请求超时

发布于 2024-12-02 07:49:12 字数 188 浏览 1 评论 0原文

我想知道是否有人可以告诉我使用express时默认的HTTP请求超时是多少。

我的意思是:当浏览器或服务器手动关闭连接时,Express / Node.js 服务器在处理 http 请求多少秒后会关闭连接?

如何更改单个路由的超时?我想将其设置为15分钟左右,用于特殊的音频转换路线。

多谢。

汤姆

I was wondering if anyone could tell me what the default HTTP request timeout is when using express.

What I mean by this is: after how many seconds of dealing with a http request will the Express / Node.js server close the connection, when the browser nor server closed the connection manually?

How do I alter this timeout for a single route? I would like to set it to about 15 minutes for a special audio conversion route.

Thanks a lot.

Tom

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

少钕鈤記 2024-12-09 07:49:12

req.connection.setTimeout(ms); 似乎是在 Node.js 中设置 HTTP 服务器的请求超时。

req.connection.setTimeout(ms); appears to set the request timeout for a HTTP server in Node.js.

罗罗贝儿 2024-12-09 07:49:12

req.connection.setTimeout(ms); 可能是一个坏主意,因为可以通过同一个套接字发送多个请求。

尝试 connect-timeout 或使用此:

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();
    };
}

req.connection.setTimeout(ms); might be a bad idea since multiple requests can be sent over the same socket.

Try connect-timeout or use this:

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();
    };
}
怎会甘心 2024-12-09 07:49:12

Node v0.9+ 中的默认请求超时为 2 分钟。这就是快递所使用的。

您可以使用以下方法增加单个路由的值:

app.get('/longendpoint', function (req, res) {
   req.setTimeout(360000); // 5 minutes
   ...
});

The default request timeout in Node v0.9+ is 2 minutes. That is what express uses.

You can increase it for a single route using:

app.get('/longendpoint', function (req, res) {
   req.setTimeout(360000); // 5 minutes
   ...
});
老子叫无熙 2024-12-09 07:49:12

连接超时或任何其他通常不起作用。
总是有效的方法是在 nginx 域/子域级别设置超时,如下所示:

location / {

                proxy_read_timeout 300;
                proxy_connect_timeout 300;
                proxy_send_timeout 300; 
   
            proxy_pass http://xxxx:3009;
        }

connect-timeout or any other generally don't work.
What always works is setting the timeout in nginx domain/subdomain level like this:

location / {

                proxy_read_timeout 300;
                proxy_connect_timeout 300;
                proxy_send_timeout 300; 
   
            proxy_pass http://xxxx:3009;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文