0
0
Node.jsframework~10 mins

Why middleware is fundamental in Node.js - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why middleware is fundamental
Incoming Request
Middleware 1: Process request
Middleware 2: Modify request/response
Middleware 3: Finalize or respond
Send Response
Request ends
Middleware functions run one after another to handle and modify requests and responses before sending the final response.
Execution Sample
Node.js
app.use((req, res, next) => {
  console.log('Middleware 1');
  next();
});

app.use((req, res, next) => {
  res.message = 'Hello';
  next();
});

app.get('/', (req, res) => {
  res.send(res.message);
});
This code shows three middleware steps: logging, adding a message, and sending the message as a response.
Execution Table
StepMiddlewareActionState ChangeNext Step
1Middleware 1Logs 'Middleware 1'No change to req or resCalls next() to Middleware 2
2Middleware 2Adds res.message = 'Hello'res.message set to 'Hello'Calls next() to Route Handler
3Route HandlerSends res.message as responseResponse sent: 'Hello'Request ends
💡 Response sent, request cycle ends
Variable Tracker
VariableStartAfter Middleware 1After Middleware 2After Route Handler
req{}{}{}{}
res.messageundefinedundefined'Hello''Hello'
Key Moments - 3 Insights
Why do we call next() in middleware?
Calling next() passes control to the next middleware or route handler, as shown in execution_table steps 1 and 2.
What happens if next() is not called?
The request stops at that middleware and never reaches the next step, so no response might be sent.
How can middleware modify the response?
Middleware can add or change properties on res, like res.message in step 2, which the route handler then uses.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does Middleware 2 do to res.message?
ASets res.message to 'Hello'
BLogs 'Middleware 2'
CSends the response immediately
DCalls next() without changes
💡 Hint
Check step 2 in the execution_table under 'State Change'
At which step is the response sent back to the client?
AMiddleware 1
BMiddleware 2
CRoute Handler
DBefore Middleware 1
💡 Hint
Look at step 3 in execution_table under 'Action'
If Middleware 1 did not call next(), what would happen?
AMiddleware 2 and Route Handler would still run
BThe request would stop and no response would be sent
CThe response would be sent immediately
DAn error would be thrown automatically
💡 Hint
Refer to key_moments about the role of next()
Concept Snapshot
Middleware is a function that runs during a request.
It can read or change request and response objects.
Must call next() to continue the chain.
Allows modular, step-by-step request handling.
Final middleware or route sends the response.
Full Transcript
Middleware in Node.js runs in sequence for each request. Each middleware can log, modify, or add data to the request or response. Calling next() moves control to the next middleware or route handler. If next() is not called, the request stops there. Middleware is fundamental because it lets us build flexible, reusable steps to process requests before sending a response.