Express.js 连接超时与服务器超时

Sca*_*aux 4 connection-timeout node.js express

我正在使用 express 和Connect Timeout Middleware来处理超时。

它工作得很好,但我默认node http服务器的超时设置为两分钟。

因此,如果我想将我的超时中间件设置为大于两分钟的值,我还必须将 http 服务器超时增加到稍大一些(否则我的连接超时处理程序不会被调用)

const app = express();
const http = new Http.Server(app);

http.setTimeout((4 * 60 * 1000) + 1); <-- Must set this

app.use(timeout('4m')); 
Run Code Online (Sandbox Code Playgroud)

我怎样才能避免这种情况?我错过了什么吗?

Mar*_*nde 6

如果要使用connect-timeout中间件,则无法避免,因为中间件不会更改套接字超时,默认为 2 分钟。

有两种可能的方法可以避免它,使用server.setTimeout()request.setTimeout

如果您只想将超时更改为几条路由,而将默认超时留给其余路由,推荐的方法是使用: request.setTimeout

app.use('/some-routes', (req, res, next) => {
   req.setTimeout((4 * 60 * 1000) + 1);
   next();
}, timeout('4m'));
Run Code Online (Sandbox Code Playgroud)

将 设置为req.setTimeout大于该connect-timeout值的替代方法是删除connect-timeout中间件并使用另一种解决方法,这也不理想。

您可以检查这个旧的 Node.js 问题https://github.com/nodejs/node-v0.x-archive/issues/3460

function haltOnTimedout (req, res, next) {
  if (!req.timedout) next()
}

app.use('/some-routes', (req, res, next) => {
    req.setTimeout(4 * 60 * 1000); // No need to offset

    req.socket.removeAllListeners('timeout'); // This is the work around
    req.socket.once('timeout', () => {
        req.timedout = true;
        res.status(504).send('Timeout');
    });

    next();
});


app.use(haltOnTimedout);

// But if the timeout occurs in the middle of a route
// You will need to check if the headers were sent or if the request timedout

app.get('/some-routes', async(req, res, next) => {
  
  // some async processing...
  await asyncOperation();
  
  if (!res.headersSent) // or !req.timedout 
    res.send('done');
 
});
Run Code Online (Sandbox Code Playgroud)