0
0
Expressframework~10 mins

next() function and flow control in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - next() function and flow control
Request received
Middleware 1
Calls next()?
Middleware 2
Calls next()?
Middleware 3
Final handler or error handler
The request passes through middleware functions one by one. Each middleware calls next() to pass control to the next middleware. If next() is not called, the flow stops and response is sent.
Execution Sample
Express
app.use((req, res, next) => {
  console.log('First middleware');
  next();
});
app.use((req, res) => {
  res.send('Done');
});
This code logs a message in the first middleware, then calls next() to continue to the next middleware which sends the response.
Execution Table
StepMiddlewareActionnext() Called?Next StepOutput
1Middleware 1Logs 'First middleware'YesMiddleware 2No output yet
2Middleware 2Sends response 'Done'NoResponse sentResponse sent: 'Done'
3EndNo more middleware--Request complete
💡 Middleware 2 does not call next(), so response is sent and flow stops.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
reqIncoming request objectPassed to Middleware 1Passed to Middleware 2Handled
resResponse objectPassed to Middleware 1Response sent with 'Done'Closed
nextFunction to call next middlewareCalled at Step 1Not called at Step 2Not called
Key Moments - 2 Insights
Why does the response get sent only after Middleware 2 and not Middleware 1?
Because Middleware 1 calls next(), it passes control to Middleware 2. Middleware 2 sends the response and does not call next(), so the flow stops there (see execution_table Step 2).
What happens if a middleware does not call next() and does not send a response?
The request will hang because the flow stops without sending a response or moving forward. This is why calling next() or sending a response is essential in each middleware.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output after Step 1?
ANo output yet
B'Done' response sent
CError message
DRequest ended
💡 Hint
Check the 'Output' column for Step 1 in the execution_table.
At which step does the response get sent?
AStep 3
BStep 1
CStep 2
DNo response sent
💡 Hint
Look at the 'Action' and 'Output' columns in execution_table rows.
If Middleware 1 did not call next(), what would happen?
AMiddleware 2 would still run
BRequest would hang without response
CResponse would be sent immediately
DAn error would be thrown
💡 Hint
Refer to key_moments about what happens if next() is not called and no response is sent.
Concept Snapshot
Express middleware functions run in order.
Each middleware receives (req, res, next).
Calling next() passes control to next middleware.
If next() is not called and no response sent, request hangs.
Sending response ends the flow.
Use next() to chain middleware or handle errors.
Full Transcript
In Express, when a request comes in, it passes through middleware functions one by one. Each middleware can do something with the request or response. To move to the next middleware, it must call the next() function. If next() is not called, the flow stops. Usually, the last middleware sends a response to the client. If a middleware neither calls next() nor sends a response, the request will hang and never finish. This flow control is important to understand to build Express apps that work correctly.