【问题标题】:Express.js HTTP request timeoutExpress.js HTTP 请求超时
【发布时间】:2011-11-05 13:16:30
【问题描述】:

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

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

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

非常感谢。

汤姆

【问题讨论】:

    标签: http node.js httprequest express


    【解决方案1】:

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

    【讨论】:

    • 这将设置连接超时而不是请求超时。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      Node v0.9+ is 2 minutes 中的默认请求超时。这就是 express 所使用的。

      您可以使用以下方法为单个路线增加它:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-12
        • 2014-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-14
        相关资源
        最近更新 更多