如何在catch块Nodejs中发送代码错误

问题描述 投票:0回答:1

我在try / catch块中遇到问题,这里是代码:

catch((err) => {
winston.error(new Date() +' : '+req.originalUrl +' : '+`${id}`+' : '+err);
res.status(500);
})

当请求失败时,调用winston的catch块在控制台中打印错误,但是请求没有停止的问题,我试图发送500代码它不起作用(这里是postman的截图,当catch块被调用时)。

enter image description here

我现在的问题是,如何发送500代码错误,并同时停止请求过程。

javascript node.js try-catch
1个回答
1
投票

试试吧

res.sendStatus(500);

代替

res.status(500);

后者仅设置响应的statusCode属性,但它不发送任何内容

© www.soinside.com 2019 - 2024. All rights reserved.