如何使用Express处理Node.js中的错误 [英] How to Handle Errors in Node.js using Express

查看:132
本文介绍了如何使用Express处理Node.js中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与项目合作伙伴合作。他在Node.js + Express中编写了很多代码,但是我们已经遇到了架构问题。



为了弥补这一点,我的主要角色是找出构建Node.js + Express应用程序的最佳方式。我遇到了两种情况,处理错误,我想要一些建议。



首先,如何捕获顶级异常?我想要的最后一件事是完全杀死节点进程的错误。我想继续为用户提供任何错误。



其次,一些错误通过回调传回(我们使用 caolan / async )。作为每个路由处理程序的一部分,我们渲染一个视图(GET),重定向到另一个路由(POST),并且我们想使用自定义错误消息重定向到错误屏幕。如何确保在一个地方捕获这个逻辑

解决方案


首先,如何捕获顶级异常?我想要的最后一件事是完全杀死节点进程的错误。我想继续为用户提供任何错误。


编辑:我认为节点的哲学一般来说,任何未捕获的异常应该杀死进程,并且您应该在具有适当日志记录功能的某种进程监视器下运行您的节点应用程序。以下建议是关于您在快速路线处理程序等中遇到的任何其他错误。



Express具有一般的errorHandler,它应该捕获所有抛出的错误以及所有传递的错误作为您的路线/中间件中 next 的参数,并以 500内部服务器错误作出回应。


其次,一些错误通过回调传回(我们使用caolan / async)。作为每个路由处理程序的一部分,我们渲染一个视图(GET),重定向到另一个路由(POST),并且我们想使用自定义错误消息重定向到错误屏幕。如何确保在一个地方捕获这个逻辑?


您可以创建一个自定义的 handleError ,您在每个回调中调用如下:

  async.series(...,function(err ,结果){
if(err)
return handleError(req,res,err);
// ...
});

或者你可以传递错误,使用 next(err)并实现您的自定义错误处理程序,如下所述: http://expressjs.com /guide/error-handling.html


I am working with a partner on a project. He has written a lot of code in Node.js+Express, but we've been running into issues with the architecture.

To remedy this, my primary role has been to figure out the best way to architect a Node.js+Express application. I've run into two scenarios, dealing with errors, and I'd like some suggestions.

First, how do I capture top-level exceptions? The last thing I want is for a bug to completely kill the node process. I want to continue serving users in the face of any error.

Secondly, some errors are passed back via callbacks (we're using caolan / async). As part of each route handler, either we render a view (GET), redirect to another route (POST) and we want to redirect to an error screen with a custom error message. How can I make sure to capture this logic in one place?

解决方案

First, how do I capture top-level exceptions? The last thing I want is for a bug to completely kill the node process. I want to continue serving users in the face of any error.

Edit: I think node's philosophy in general is that any uncaught exceptions should kill the process, and that you should run your node app under some kind of process monitor with appropriate logging facilities. The following advice is regarding any other errors you might encounter in your express route handlers etc.

Express has a general errorHandler, which should capture all thrown errors as well as everything passed as a parameter to next in your routes/middlewares, and respond with 500 Internal Server Error.

Secondly, some errors are passed back via callbacks (we're using caolan / async). As part of each route handler, either we render a view (GET), redirect to another route (POST) and we want to redirect to an error screen with a custom error message. How can I make sure to capture this logic in one place?

You could create a custom handleError, which you call in each callback like so:

async.series(..., function(err, results) {
  if(err)
    return handleError(req, res, err);
  // ...
});

Or you could just pass the errors on with next(err) and implement your custom error handler as described here: http://expressjs.com/guide/error-handling.html

这篇关于如何使用Express处理Node.js中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆