0
0
Expressframework~10 mins

Middleware execution flow (req, res, next) in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Middleware execution flow (req, res, next)
Request Received
Middleware 1
Calls next()
Middleware 2
Calls next()
Middleware 3
Sends Response
Response Sent
The request passes through each middleware in order. Each middleware can call next() to pass control to the next one or send a response to end the chain.
Execution Sample
Express
app.use((req, res, next) => {
  console.log('First middleware');
  next();
});
app.use((req, res, next) => {
  console.log('Second middleware');
  res.send('Done');
});
Two middlewares run in order; the first logs and calls next(), the second logs and sends a response.
Execution Table
StepMiddlewareActionnext() Called?Response Sent?Console Output
1Middleware 1Logs 'First middleware'YesNoFirst middleware
2Middleware 2Logs 'Second middleware' and sends responseNoYesSecond middleware
3EndResponse sent, no further middlewareN/AYesN/A
💡 Response sent in Middleware 2, so middleware chain stops.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
reqIncoming request objectPassed to Middleware 2Passed to Middleware 2Used to send response
resResponse objectNot sentResponse sentResponse sent
nextFunction to call next middlewareCalledNot calledN/A
Key Moments - 3 Insights
Why does the second middleware run after the first?
Because the first middleware calls next(), which passes control to the next middleware as shown in execution_table step 1.
What happens if a middleware sends a response without calling next()?
The middleware chain stops immediately, no further middleware runs, as seen in execution_table step 2 and exit_note.
Can next() be called multiple times in one middleware?
No, calling next() more than once can cause errors or unexpected behavior. Each middleware should call next() once or send a response.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the console output after step 1?
A'First middleware'
B'Second middleware'
C'Done'
DNo output yet
💡 Hint
Check the 'Console Output' column for step 1 in execution_table.
At which step does the response get sent?
AStep 1
BStep 3
CStep 2
DNo response sent
💡 Hint
Look at the 'Response Sent?' column in execution_table.
If Middleware 1 did not call next(), what would happen?
AResponse would be sent immediately
BMiddleware chain would stop after Middleware 1
CMiddleware 2 would still run
DAn error would occur
💡 Hint
Refer to the concept_flow and key_moments about next() usage.
Concept Snapshot
Middleware in Express runs in order.
Each middleware gets req, res, next.
Call next() to pass control to next middleware.
Send response to end chain.
If next() not called and no response sent, request hangs.
Full Transcript
In Express, middleware functions run one after another when a request comes in. Each middleware receives three things: the request object, the response object, and a next function. When a middleware finishes its job, it can call next() to let the next middleware run. If it sends a response instead, the chain stops there. This flow ensures that requests are handled step-by-step, and responses are sent only once. The execution table shows how the first middleware logs a message and calls next(), then the second middleware logs another message and sends the response, stopping the chain.