Express.js HTTP请求超时 [英] Express.js HTTP request timeout

查看:318
本文介绍了Express.js HTTP请求超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道有没有人可以告诉我使用express时默认的HTTP请求超时是什么。

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

我的意思是:经过多少秒的交易用http请求将Express / Node.js服务器关闭连接,当浏览器或服务器手动关闭连接时?

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?

如何更改单个路由的超时?我想设置一个特定的音频转换路线约15分钟。

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.

非常感谢。

Tom

推荐答案

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

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

尝试连接超时或使用这个:

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.js HTTP请求超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆