Skip to content

Instantly share code, notes, and snippets.

@LRagji
Created February 21, 2019 11:09
Show Gist options
  • Save LRagji/27825221c46cca8f120e0645d31ca365 to your computer and use it in GitHub Desktop.
Save LRagji/27825221c46cca8f120e0645d31ca365 to your computer and use it in GitHub Desktop.
Pro-Tips: Pattern for Express(Node) with error handling
var app = express();
//All routes to hook it up to
app.use('/', rootModule);
app.use('/module1', module1);
app.use('/module2', module2);
//Handle other requests that doesnot fall through above routes
app.use(function (req, res, next) {
next(createError(404));
});
//Finally handle any global errors(un-handelled) here(check err in handler)
app.use(function (err, req, res, next) {
// render the error page or alike
res.status(err.status || 500);
res.send(req.app.get('env') === 'development' ? err : {});
});
module.exports = app;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment