0
0
Expressframework~10 mins

Why middleware is Express's core concept - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why middleware is Express's core concept
Incoming HTTP Request
Middleware 1: Process Request
Middleware 2: Modify Request/Response
Middleware 3: Route Handling or Next Middleware
Send Response or Pass to Next Middleware
End
Express processes each HTTP request by passing it through a chain of middleware functions that can modify the request or response, or decide to end the response.
Execution Sample
Express
app.use((req, res, next) => {
  console.log('Middleware 1');
  next();
});

app.use((req, res, next) => {
  res.send('Hello from Middleware 2');
});
This code shows two middleware functions: the first logs a message and calls next(), the second sends a response ending the chain.
Execution Table
StepMiddlewareActionOutputNext Called?
1Middleware 1Logs 'Middleware 1'No response sentYes
2Middleware 2Sends response 'Hello from Middleware 2'Response sent to clientNo
3EndNo more middleware calledRequest handling endsN/A
💡 Middleware 2 sends a response, so the chain stops here.
Variable Tracker
VariableStartAfter Middleware 1After Middleware 2Final
reqIncoming HTTP request objectPassed unchangedPassed unchangedUsed to send response
resResponse objectPassed unchangedResponse sent with 'Hello from Middleware 2'Response completed
nextFunction to call next middlewareCalled to continueNot calledChain ends
Key Moments - 2 Insights
Why does the second middleware stop the chain?
Because it sends a response with res.send(), Express stops calling further middleware as the response is complete (see execution_table step 2).
What happens if a middleware does not call next() or send a response?
The request will hang because Express waits for either next() to continue or a response to end the chain. Middleware 1 calls next() to avoid this (see execution_table step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what does Middleware 1 do at step 1?
ASends a response to the client
BEnds the request without response
CLogs a message and calls next()
DThrows an error
💡 Hint
Check the 'Action' and 'Next Called?' columns in execution_table row 1.
At which step does Express stop calling middleware?
AStep 2
BStep 1
CStep 3
DNever stops
💡 Hint
Look at the 'Next Called?' column in execution_table; it is 'No' at step 2.
If Middleware 1 did not call next(), what would happen?
AMiddleware 2 would still run
BThe request would hang without response
CExpress would send a default response
DAn error would be thrown automatically
💡 Hint
Refer to key_moments about what happens if next() is not called.
Concept Snapshot
Express middleware are functions that process requests in order.
Each middleware can modify request/response or end the response.
Calling next() passes control to the next middleware.
Sending a response stops the chain.
Middleware chaining is core to Express's request handling.
Full Transcript
Express uses middleware functions to handle HTTP requests step-by-step. Each middleware receives the request and response objects and can either modify them, send a response, or pass control to the next middleware by calling next(). If a middleware sends a response, Express stops calling further middleware. If next() is not called and no response is sent, the request will hang. This chaining of middleware functions is the core concept of Express, allowing flexible and modular request processing.