码迷,mamicode.com
首页 > Web开发 > 详细

nodejs中处理回调函数的异常

时间:2017-06-01 17:47:10      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:经典的   函数   ping   res   dump   led   ati   tac   process   

假设是使用nodejs+express3这个经典的组合。那么有一种非常方面的处理回调函数异常的方法:


1. 安装模块:express-domain-middleware

2. 增加例如以下的代码:

app.use(require(‘express-domain-middleware‘));
app.use(function errorHandler(err, req, res, next) {
  logger.error(‘error on request %d %s %s: %j‘, process.domain.id, req.method, req.url, err);
  res.send(500, "there is an error in callback function");
  if(err.domain) {
    //you should think about gracefully stopping & respawning your server
    //since an unhandled error might put your application into an unknown state
  }
});
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));



nodejs中处理回调函数的异常

标签:经典的   函数   ping   res   dump   led   ati   tac   process   

原文地址:http://www.cnblogs.com/gccbuaa/p/6929827.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!