CodeZettaInterview Hub

Node.js · Express Concepts · Mid-Level · Live Coding

Error-handling middleware signature?

Short Interview Answer

Four args: (err, req, res, next) — Express detects by arity.

Detailed Explanation

Must be registered after routes. Map domain errors to HTTP statuses. Avoid leaking stack traces in production.

Example

app.use((err, req, res, next) => {
  res.status(err.status || 500).json({ error: err.message });
});

Did you know this answer?