0
0
NestJSframework~10 mins

Creating middleware in NestJS - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating middleware
Request comes in
Middleware runs
Middleware can modify request/response
Call next() to continue
Controller handles request
Response sent back
Middleware runs when a request arrives, can change request or response, then passes control to the next step.
Execution Sample
NestJS
import { Injectable, NestMiddleware } from '@nestjs/common';

@Injectable()
export class LoggerMiddleware implements NestMiddleware {
  use(req, res, next) {
    console.log(`Request to: ${req.url}`);
    next();
  }
}
This middleware logs the URL of each incoming request and then passes control to the next handler.
Execution Table
StepActionRequest URLConsole OutputNext CalledEffect
1Request arrives/homeNoMiddleware starts
2Middleware logs URL/homeRequest to: /homeNoLogging happens
3Middleware calls next()/homeRequest to: /homeYesPass control to controller
4Controller handles request/homeRequest to: /homeYesResponse prepared
5Response sent/homeRequest to: /homeYesClient receives response
💡 Middleware calls next(), so request proceeds to controller and response is sent.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
req.url/home/home/home/home
console outputRequest to: /homeRequest to: /homeRequest to: /home
next calledNoNoYesYes
Key Moments - 2 Insights
Why do we need to call next() inside middleware?
Calling next() tells NestJS to continue processing the request. Without it, the request stops at middleware and the controller never runs, as shown in execution_table step 3.
Can middleware change the request or response?
Yes, middleware can modify req or res objects before passing control. This is why middleware runs before the controller, as seen in the flow diagram.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the console output at step 2?
ARequest to: /home
BNo output yet
Cnext() called
DResponse sent
💡 Hint
Check the 'Console Output' column at step 2 in the execution_table.
At which step does the middleware pass control to the controller?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Next Called' column to see when it changes to Yes.
If next() was not called in middleware, what would happen?
AResponse would be sent twice
BRequest would skip middleware
CRequest would stop at middleware and controller won't run
DMiddleware would run twice
💡 Hint
Refer to the key moment about why next() is needed and execution_table step 3.
Concept Snapshot
Creating middleware in NestJS:
- Middleware runs on each request before controllers
- Implement NestMiddleware interface with use(req, res, next)
- Call next() to continue processing
- Can modify req or res objects
- Useful for logging, auth, or modifying requests
Full Transcript
Middleware in NestJS runs when a request arrives. It can look at or change the request or response. The middleware must call next() to let the request continue to the controller. If next() is not called, the request stops and the controller never runs. For example, a LoggerMiddleware logs the request URL and then calls next(). The flow is: request arrives, middleware runs and logs, calls next(), controller handles request, response is sent back. This helps add features like logging or authentication before the main handling.