0
0
NestJSframework~10 mins

Functional middleware in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Functional middleware
Request Received
Middleware Function Called
Middleware Logic Executes
Call next() to pass control
Next Middleware or Handler Executes
Response Sent
The request passes through the functional middleware which runs its logic and calls next() to continue the flow.
Execution Sample
NestJS
function logger(req, res, next) {
  console.log(`Request to ${req.url}`);
  next();
}
Logs the request URL and passes control to the next middleware or handler.
Execution Table
StepActionRequest URLConsole Outputnext() CalledNext Step
1Middleware function invoked/api/dataNoMiddleware logic runs
2Log request URL/api/dataRequest to /api/dataNoMiddleware logic continues
3Call next()/api/dataRequest to /api/dataYesNext middleware or handler runs
4Next middleware or handler executes/api/dataYesResponse sent
5Response sent to client/api/dataYesEnd
💡 Middleware calls next(), passing control to next middleware or handler, ending middleware execution.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
req.url/api/data/api/data/api/data/api/data
console outputRequest to /api/dataRequest to /api/dataRequest to /api/data
next() calledNoNoNoYesYes
Key Moments - 2 Insights
Why do we need to call next() inside the middleware?
Calling next() passes control to the next middleware or route handler. Without it, the request would hang and never reach the handler, as shown in execution_table step 3.
What happens if we don't call next()?
The request stops at the middleware and the client waits forever. The execution_table shows next() called as 'No' means no further processing.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the console output at step 2?
A"Request received"
B"Middleware started"
C"Request to /api/data"
D"No output"
💡 Hint
Check the 'Console Output' column at step 2 in the execution_table.
At which step is next() called to continue processing?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'next() Called' column in the execution_table.
If next() was not called, what would happen to the request?
AIt would hang and never reach the handler
BIt would immediately send a response
CIt would continue to the next middleware
DIt would throw an error automatically
💡 Hint
Refer to the key_moments section and execution_table exit_note.
Concept Snapshot
Functional middleware in NestJS is a function with (req, res, next).
It runs code, then calls next() to pass control.
Without next(), request processing stops.
Use middleware to log, modify requests, or check auth.
Always call next() unless sending a response directly.
Full Transcript
Functional middleware in NestJS is a simple function that takes three arguments: request, response, and next. When a request comes in, NestJS calls this middleware function. Inside, you can run any code you want, like logging the request URL. After your code runs, you must call next() to pass control to the next middleware or the route handler. If you forget to call next(), the request will hang and never reach the handler. This flow ensures middleware can process requests step-by-step before the final response is sent.