Created
February 21, 2019 11:09
-
-
Save LRagji/27825221c46cca8f120e0645d31ca365 to your computer and use it in GitHub Desktop.
Pro-Tips: Pattern for Express(Node) with error handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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