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

时间:2019-03-26 18:53:26

标签: node.js express connection-timeout

我正在使用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')); 

如何避免这种情况?我想念什么吗?

1 个答案:

答案 0 :(得分:1)

如果要使用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'));

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');

});
相关问题