开发者

Express.js HTTP request timeout

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 开发者_C百科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


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


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


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


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;
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜