来定义多个这样的处理程序并将其链接在一起
我正试图在我的节点项目上创建一个错误处理程序,但我不明白什么。
对于抛出的错误,我可以从两种不同的方式捕捉:
process.on()
process.on('uncaughtException', function(err) {
// Nothing to do... Client request can't be closed
});
app.use()
app.use(function(err, req, res, next) {
res.send("Humm... To bad !", 500);
});
我正在使用一个带有RESTful API的函数:
app.use(function(req, res, next) {
// throw new Error("This error is caught by app.use()");
api.getData(function(err, result) {
if (err) {
// throw new Error("This error is caught by process.on()");
}
/* Some code here */
});
});
我真的不明白两者有什么区别。。我不喜欢这个过程。on()方式,在这个陷阱中,我无法访问req和res来向客户端发送500错误页面。。
process.on()将处理进程中任何未捕获的错误,其中as-app.use是处理请求处理错误的正确方法。您还可以通过调用next(err)