InterviewHubPRO
Section Overview
Q38

Question 38 of 60

Part 4: Express, Fastify, APIs & Middleware (36 - 50)

63%
Node.js & Libuv•Part 4: Express, Fastify, APIs & Middleware (36 - 50)

Centralized Global Error Handler in Express.

In Simple Words: Must accept exactly 4 parameters (err, req, res, next) so Express's reflection inspects fn.length === 4 to recognize it as an error-handling middleware:

javascript
app.use((err, req, res, next) => {
  console.error('Global Error Handler:', err.stack);
  res.status(err.statusCode || 500).json({
    error: { message: err.message, status: err.statusCode || 500 }
  });
});