Last active
December 25, 2019 14:11
-
-
Save songokjesse/9367367eca14e539838e924d9879ea27 to your computer and use it in GitHub Desktop.
Express Error Handling Middleware
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
//catch 404 and forward to error handler | |
app.use((req,res,next)=>{ | |
let err = new Error('Not Found'); | |
err.status = 404; | |
next(err); | |
}); | |
// error handler | |
app.use((err,req,res,next)=>{ | |
res.status(err.status || 500); | |
res.json({ | |
message: err.message, | |
error: req.app.get('env') == 'development' ? err : {} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My go to Error Handling middleware when using Express Nodejs