To setup global error handling with Node.js express is basically very easy.

The important part is to define all four arguments (err, req, res, next). Otherwise the error handler function will never get triggered.

const express = require('express');
const app = express();

// Order matters. Other middleware before the error handler...

app.use(function (err, req, res, next) {
  res
    .status(err.status || 500)
    .send({ message: err.message, stack: err.stack });
});