0
0
Node.jsframework~10 mins

Middleware concept and execution flow in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Middleware concept and execution flow
Request Received
Middleware 1
Middleware 2
Middleware 3
Route Handler
Response Sent
The request passes through each middleware in order, each can modify the request or response, then finally the route handler sends the response.
Execution Sample
Node.js
app.use((req, res, next) => {
  console.log('Middleware 1');
  next();
});

app.use((req, res, next) => {
  console.log('Middleware 2');
  next();
});

app.get('/', (req, res) => {
  res.send('Hello World');
});
This code shows two middleware functions logging messages, then a route handler sending a response.
Execution Table
StepActionMiddleware/HandlerConsole OutputNext CalledResponse Sent
1Request receivedN/AYesNo
2Middleware 1 runsMiddleware 1Middleware 1YesNo
3Middleware 2 runsMiddleware 2Middleware 2YesNo
4Route handler runsRoute HandlerN/AYes
5Response sent to clientN/AN/AYes
💡 Response sent, middleware chain ends
Variable Tracker
VariableStartAfter Middleware 1After Middleware 2After Route Handler
req{original request}{possibly modified}{possibly modified}{final state}
res{original response}{unchanged}{unchanged}{response sent}
nextfunctioncalledcallednot called
Key Moments - 2 Insights
Why does the middleware need to call next()?
Calling next() passes control to the next middleware or route handler. Without it, the request stops and the response is never sent, as shown in steps 2 and 3 where next() is called to continue.
What happens if a middleware sends a response without calling next()?
If a middleware sends a response and does not call next(), the chain stops immediately and no further middleware or route handlers run. This prevents duplicate responses.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is logged to the console at step 3?
AMiddleware 1
BMiddleware 2
CRoute Handler
DNo output
💡 Hint
Check the 'Console Output' column at step 3 in the execution table.
At which step is the response sent to the client?
AStep 5
BStep 2
CStep 4
DStep 3
💡 Hint
Look at the 'Response Sent' column in the execution table.
If Middleware 1 does not call next(), what happens?
AResponse is sent immediately
BMiddleware 2 and route handler run as usual
COnly Middleware 1 runs, then request stops
DAn error is thrown automatically
💡 Hint
Refer to the key moment about calling next() and the execution flow.
Concept Snapshot
Middleware functions run in order on each request.
Each middleware receives req, res, and next.
Call next() to pass control to next middleware.
If next() is not called, request stops.
Route handler sends the final response.
Middleware can modify req/res or end response early.
Full Transcript
Middleware in Node.js is a function that runs during the request-response cycle. When a request comes in, it passes through each middleware in order. Each middleware can do something with the request or response, then calls next() to pass control to the next middleware. If next() is not called, the chain stops and the response may never be sent. Finally, the route handler sends the response back to the client. This flow ensures modular and manageable code for handling requests.