Skip to content

Instantly share code, notes, and snippets.

@jhaashutosh
Created January 11, 2023 14:36
Show Gist options
  • Save jhaashutosh/509ad31461bb5b8ca31121fe80c66d66 to your computer and use it in GitHub Desktop.
Save jhaashutosh/509ad31461bb5b8ca31121fe80c66d66 to your computer and use it in GitHub Desktop.
const notFoundHandler = (req, res, next) => {
const error = new Error(`Not found - ${req.originalUrl}`);
res.status(404);
next(error);
};
const errorHandler = (err, req, res, next) => {
const statusCode = res.statusCode === 200 ? 500 : res.statusCode; //we sometimes get 200 code (ok) even if its error. 500 means server error
res.status(statusCode);
res.json({
message: err.message,
stack: process.env.NODE_ENV === "production" ? null : err.stack,
});
};
module.exports = { notFoundHandler, errorHandler };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment