Untitled Lesson
Skill Matrix
UNLOCK NODES BY LEARNING NEW TAGS.
AI-generated Express error-handling middleware is registered with a `(req, res, next)` signature instead of `(err, req, res, next)`. What is the practical consequence?
💻 Code Challenge | +75 XP
Write a prompt requesting an Express route for order creation that explicitly specifies: wrap in an existing asyncHandler utility, use express-validator matching an existing style, and register required middleware in the correct order.
A newly generated Express endpoint hangs indefinitely on invalid input instead of returning a 400 error. Reorder the steps to diagnose and fix the likely cause.
Task: Reorder the blocks in logical sequence to solve the problem.
A.D.A. Interface
Adaptive Didactic Assistant

Pascual Vila
Frontend Instructor // Code Syllabus
The Error //
Registering AI-generated error-handling middleware with the wrong number of parameters
// Wrong: Express does NOT treat this as an error handler
app.use((req, res, next) => { /* meant to handle errors, but never will */ });
// Correct: exactly 4 parameters
app.use((err, req, res, next) => { res.status(500).json({ error: err.message }); });The Solution //
Express specifically identifies error-handling middleware by its exact 4-parameter signature (err, req, res, next) — anything else, even something intended as error handling, is treated as regular middleware and will never be invoked when an error actually occurs, silently leaving errors unhandled.
The Error //
Registering body-parsing middleware after routes that need to read req.body
// Wrong: route runs before the body is ever parsed
app.post("/orders", createOrder);
app.use(express.json());
// Correct: parser registered first
app.use(express.json());
app.post("/orders", createOrder);The Solution //
Express middleware executes in registration order — a route registered before express.json() (or a similar body parser) will find req.body undefined, since the parsing hadn't happened yet when that route runs. Body-parsing middleware must be registered before any route that depends on it.