如何在请求超时时停止Express中的进程?
安装'connect-timeout‘
import timeout from 'connect-timeout';
// halt_on_timeout.js
module.exports = function haltOnTimedout(req, res, next) {
if (!req.timedout) next();
};昂贵的进程在路由超时后花费的时间超过30秒,但expensive_long_operation从不停止运行。
route.post(
'upload_alot_of_content/'
timeout('30s'),
haltOnTimedout,
async (req, res) => {
const result = await expensive_long_operation();
if (req.timedout) {
next('error!')
}
res({....})
})发布于 2019-08-28 04:56:42
在nodejs中无法任意停止异步进程,但是您可以实现一些选项来实现您想要的功能。
https://stackoverflow.com/questions/57681046
复制相似问题