Untitled Lesson
Skill Matrix
UNLOCK NODES BY LEARNING NEW TAGS.
What happens to the user
š» Code Challenge | +75 XP
Write the Node.js/Backend code snippet to implement a Node Middleware Pattern pipeline. Include the setup and basic execution steps.
You are reviewing a Node Middleware Pattern pipeline and the output is incorrect. Reorder the following pipeline stages in the correct logical order to fix the bug: Input Data, Process, Output.
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 //
Forgetting to call next() (or send a response) inside a middleware function
// Wrong: request hangs forever
const logger = (req, res, next) => {
console.log(req.url);
// missing next()!
};
// Correct
const logger = (req, res, next) => {
console.log(req.url);
next();
};The Solution //
If a middleware neither calls next() nor sends a response with res.send()/res.json()/res.end(), Express has no instruction to move forward, and the client's request hangs until it eventually times out. Every code path in a middleware must either terminate the response or call next() ā including inside if/else branches and catch blocks.
The Error //
Calling next() after already sending a response ("Cannot set headers after they are sent")
// Wrong: sends a response, then falls through to next()
const requireAuth = (req, res, next) => {
if (!req.user) {
res.status(401).json({ error: 'Unauthorized' });
}
next(); // still runs even after the response was sent!
};
// Correct: return stops execution here
const requireAuth = (req, res, next) => {
if (!req.user) {
return res.status(401).json({ error: 'Unauthorized' });
}
next();
};The Solution //
If a middleware calls res.send() or res.json() and then also calls next() (or calls next() twice), the request continues down the pipeline and a later handler tries to write to a response that's already closed, throwing the ERR_HTTP_HEADERS_SENT error. Always `return` immediately after sending a response so the function exits before reaching any subsequent next() call.