so I've recently started programing in node and MEAN and while working with JAVA I would always focus on best pattern to create solid apps, and even though the learning curve is killing me I'm trying the same here. Anyway I'm trying to implement a generic error handler using MEANjs to have an exception handler in a central place, log errors to a file and send email when this happens.
starting with mongoose for example
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});
});
};);
this is the handler in 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});
});
so here is where it gets confusing since I'm trying to implemente a middleware with express I'm not sure if app.use is the best aproach, I've read that sometimes is not the best aproach, or this is just for Express 3.xx?? what about express 4? below is what I'm using for the logger file and then using it before the express error handler is this good practice or it should go inside the handler?
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}}'
});
So landing this. is there a way to have a general errors handler via express 4 using MEANJS sending emails when they happen logging into a file. and get rid of all this repetitive mongoose errors handling keeping in mind best practices for MEANJS.