0
0
NestJSframework~10 mins

Middleware ordering in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Middleware ordering
Request Received
Middleware 1 Executes
Middleware 2 Executes
Route Handler Executes
Response Sent Back
Middleware run in the order they are registered. Each middleware processes the request before passing it on.
Execution Sample
NestJS
app.use(middlewareOne);
app.use(middlewareTwo);

// Request -> middlewareOne -> middlewareTwo -> handler
Shows how two middlewares run in sequence before the route handler.
Execution Table
StepMiddlewareActionNext Step
1middlewareOneProcesses request, calls next()Pass to middlewareTwo
2middlewareTwoProcesses request, calls next()Pass to route handler
3Route HandlerHandles request, sends responseResponse sent
4-Request cycle completeEnd
💡 All middlewares called next(), request reached handler and response sent
Variable Tracker
VariableStartAfter middlewareOneAfter middlewareTwoFinal
request.processedBy[][middlewareOne][middlewareOne, middlewareTwo][middlewareOne, middlewareTwo, handler]
Key Moments - 2 Insights
Why does middlewareTwo run after middlewareOne?
Because middleware are called in the order they are registered, as shown in execution_table steps 1 and 2.
What happens if a middleware does not call next()?
The request stops there and does not reach the next middleware or handler, so response is never sent. This is why calling next() is crucial.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 2?
AmiddlewareTwo processes request and calls next()
BmiddlewareOne sends response
CRoute handler executes
DRequest ends without response
💡 Hint
Check execution_table row with Step 2 for middlewareTwo action
At which step does the route handler execute?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at execution_table row where Middleware is 'Route Handler'
If middlewareOne did not call next(), what would happen?
AmiddlewareTwo would still run
BRoute handler would run immediately
CRequest would stop at middlewareOne
DResponse would be sent twice
💡 Hint
Refer to key_moments about importance of calling next()
Concept Snapshot
Middleware ordering in NestJS:
- Middleware run in registration order
- Each middleware must call next() to continue
- Request flows through all middlewares before handler
- Missing next() stops request chain
- Order affects processing and response timing
Full Transcript
In NestJS, middleware are functions that run in the order they are registered. When a request comes in, it first passes through the first middleware, which can process or modify the request, then calls next() to pass control to the next middleware. This continues until all middlewares have run, then the route handler executes to send the response. If any middleware does not call next(), the request stops there and the response is not sent. This flow is shown step-by-step in the execution table and variable tracker, illustrating how the request is processed sequentially. Understanding this order helps avoid bugs and ensures middleware behave as expected.