ExpressJsError Handling Middleware

Handle errors globally by creating a custom middleware that captures errors and returns a proper response to the client.

app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).send('Something went wrong!');
});