code: function error(err, req, res, next) {
    // log it
    if (!test) console.error(err.stack);
  
    // respond with 500 "Internal Server Error".
    res.status(500);
    res.send('Internal Server Error');
  }
docs: error handling middleware have an arity of 4 instead of the typical (req, res, next), otherwise they behave exactly like regular middleware, you may have several of them, in different orders etc.