Skip to content

Instantly share code, notes, and snippets.

@songokjesse
Last active December 25, 2019 14:11
Show Gist options
  • Save songokjesse/9367367eca14e539838e924d9879ea27 to your computer and use it in GitHub Desktop.
Save songokjesse/9367367eca14e539838e924d9879ea27 to your computer and use it in GitHub Desktop.
Express Error Handling Middleware
//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 : {}
});
});
@songokjesse
Copy link
Author

songokjesse commented Dec 25, 2019

My go to Error Handling middleware when using Express Nodejs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment