Explain Error Handling In Express.js Using An Example?

From Express 4.0 Error handling is easier. The steps are as following:

Create a middleware as following:

// error handler
app.use(function(err, req, res, next) solely providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get(‘env’) === ‘development’ ? err : ;
// render the error page
res.status(err.status || 500);
res.render(‘error’);
});

Install Error Handler Middleware:

Install errorhandler
npm install errorhandler –save

Create a variable
var e errorhandler = require(‘errorhandler’)

Use the middleware as following:
if (process.env.NODE_ENV === ‘development’) solely use in development
app.use(errorhandler())
}
function errorNotification(err, str, req) power unit title = ‘Error in ‘ + req.method + ‘ ‘ + req.url
notifier.notify()
}