gpt4 book ai didi

javascript - Express中的JS运行时错误

转载 作者:行者123 更新时间:2023-12-03 08:39:50 24 4
gpt4 key购买 nike

我有一个基本的Express应用,其中包含一些路线。
一种是这样的:

router.post('/upload', (req, res) => {
let audioFile = req.files.audioFile;

const file = __dirname + '/../' + req.body.uploadLocation + audioFile.name;

mp3.mv(file, function(err) { // <--- HERE
if (err) {
return res.status(500).send(err);
}

res.send('File uploaded!');
});
});
现在,如果您尝试将文件上传到此路由,则会得到一个 500就是这样。高亮显示的行是我更改变量名称的结果。有什么办法可以使它打印出实际错误吗?我经常被这个问题绊倒,如果终端输出告诉我正常的JS错误(如在浏览器中或当我正常运行node时),它将使速度更快。

最佳答案

在Express中,有用于处理错误的中间件。在express的基本设置中,您将找到:

// error handler
app.use(function(err, req, res, next) {
...
// render the error page
res.status(err.status || 500);
res.json({ error: err });
});
只需在其中添加一个 console.error:
// error handler
app.use(function(err, req, res, next) {
console.error(err.stack); // <- HERE
...
// render the error page
res.status(err.status || 500);
res.json({ error: err });
});

关于javascript - Express中的JS运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62667207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com