0
0
NestJSframework~10 mins

Global middleware in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Global middleware
Start Application
Register Global Middleware
Incoming Request
Global Middleware Executes
Pass to Route Handler
Send Response
End Request
The app starts, global middleware is registered, then for every request the middleware runs before the route handler, finally the response is sent.
Execution Sample
NestJS
import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';

@Injectable()
export class LoggerMiddleware implements NestMiddleware {
  use(req: Request, res: Response, next: NextFunction) {
    console.log(`Request to: ${req.url}`);
    next();
  }
}

// In main.ts
app.use(new LoggerMiddleware().use.bind(new LoggerMiddleware()));
This code defines a global middleware that logs each request URL before passing control to the next handler.
Execution Table
StepActionRequest URLMiddleware LogNext CalledRoute Handler Called
1Incoming request received/homeNoNo
2Middleware runs/homeRequest to: /homeYesNo
3Route handler runs/homeRequest to: /homeYesYes
4Response sent/homeRequest to: /homeYesYes
5Request ends/homeRequest to: /homeYesYes
💡 Request completes after middleware and route handler finish processing.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
req.urlundefined/home/home/home/home
Middleware LogemptyemptyRequest to: /homeRequest to: /homeRequest to: /home
next() calledfalsefalsetruetruetrue
Route Handler Calledfalsefalsefalsetruetrue
Key Moments - 2 Insights
Why does the middleware need to call next()?
Calling next() tells NestJS to continue to the next step, usually the route handler. Without next(), the request would hang and never reach the handler, as shown in step 2 of the execution_table.
Is the middleware run for every request?
Yes, global middleware runs for every incoming request before any route handler, as seen in the concept_flow and execution_table steps 1 and 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of Middleware Log at step 2?
A"" (empty string)
B"Request to: /home"
Cundefined
Dnull
💡 Hint
Check the Middleware Log column at step 2 in the execution_table.
At which step does the route handler start executing?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the Route Handler Called column in the execution_table.
If next() was not called in middleware, what would happen?
ARequest would hang and not reach route handler
BRoute handler would still run
CMiddleware would run twice
DResponse would be sent immediately
💡 Hint
Refer to the key_moments explanation about next() and step 2 in execution_table.
Concept Snapshot
Global middleware runs for every request before route handlers.
Define middleware class with use(req, res, next).
Call next() to continue processing.
Register globally with app.use(middleware).
Useful for logging, auth, or modifying requests.
Full Transcript
Global middleware in NestJS runs for every incoming request before the route handler. It is defined as a class implementing NestMiddleware with a use method that receives request, response, and next function. The middleware can perform actions like logging the request URL, then calls next() to pass control to the next handler. If next() is not called, the request will hang and not proceed. Global middleware is registered in main.ts using app.use(). This ensures consistent behavior for all routes, such as logging or authentication checks.