【问题标题】:Avoid abort of http requests that take long time in Node.js/Express server避免在 Node.js/Express 服务器中中止需要很长时间的 http 请求
【发布时间】:2020-09-03 15:22:23
【问题描述】:

我在我的 React/Nodejs 应用程序中遇到了 http 请求问题。当我尝试在向客户端发送响应之前执行需要很长时间(例如 4 分钟或更长时间)的请求时,请求被中止。请求是否有超时或我不考虑的事情? 这是我的 POST 请求的响应:Failed to load resource: net::ERR_EMPTY_RESPONSE

AbortController 有什么需要处理的吗?

【问题讨论】:

  • 我猜你的服务器正在杀死 http 请求,而不是客户端。
  • 发送 http 请求后,我在服务器中启动批处理过程,就像在 try catch 块await spawn(os_shell, ['/c', runFile], { stdio: 'inherit' }); 中使用节点中的 await-spawn 模块一样。
  • 快递或您正在运行的任何东西的超时时间是多少?
  • 是的,我使用的是快递。如何检查或设置超时?

标签: javascript node.js express


【解决方案1】:

编辑:查看我的答案标题以解决问题。 我在我的 Express.js 中找到了使用 setTimeout() 的解决方案。问题是由于默认快速超时导致服务器中止。

我在我的服务器中添加了这段代码来解决它。希望这对某人有用:

const timeOut = 1000 * 60 * 10;
app.use((req, res, next) => {
    req.setTimeout(timeOut, () => {
        const error = new Error('Request Timeout.');
        err.status = 408;
        next(error);
    });
    res.setTimeout(timeOut, () => {
        const error = new Error('Request has expired.');
        err.status = 503;
        next(error);
    })
    next();
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-10
    • 2018-01-15
    • 2020-03-19
    • 2018-07-31
    • 2020-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多