【发布时间】:2014-09-25 18:45:41
【问题描述】:
所以我最近开始使用 node 和 MEAN 进行编程,并且在使用 JAVA 工作时,我会始终专注于创建可靠应用程序的最佳模式,尽管学习曲线让我很受挫,但我在这里也尝试了同样的方法。无论如何,我正在尝试使用MEANjs 实现一个通用错误处理程序,以便在中心位置有一个异常处理程序,将错误记录到文件并在发生这种情况时发送电子邮件。
以猫鼬为例
app.route('/process/:pId').get(function(req, res){
pModel.findById(req.params.pId, function(err, doc){
if(err) {//this is so
console.log(err);//repetitive so I would
res.send(500, 'Error: error.');
}//like to send all these errors to a central handler like in express.js below
res.json(200, {data: doc});
});
};);
这是 express.js 中的处理程序:
app.use(function(err, req, res, next) {
if (!err) return next();
// can I call another error handlers at this level??
console.error(err.stack);
// Error page
res.status(500).render('500', { error: err.stack});
});
所以这是令人困惑的地方,因为我正在尝试使用 express 实现中间件我不确定 app.use 是否是最好的方法,我有 read 这有时不是最好的方法,或者这仅适用于 Express 3.xx?快递4呢?下面是我用于记录器文件的内容,然后在快速错误处理程序之前使用它是这种好习惯还是应该进入处理程序?
var logger = expressWinston.logger({
transports:[
new (winston.transports.File)({
filename:'./app/log/winston.log',
colorize: false,
json: true
})
],
statusLevels:true,
meta: true,
msg: 'HTTP {{req.method}} {{req.url}}'
});
所以登陆这个。有没有办法通过 express 4 使用 MEANJS 在登录文件时发送电子邮件来获得一般错误处理程序。并摆脱所有这些重复的猫鼬错误处理,牢记 MEANJS 的最佳实践。
【问题讨论】:
标签: node.js express mongoose winston meanjs