0
0
NestJSframework~10 mins

Decorator-based architecture in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Decorator-based architecture
Define Class
Apply Decorators
NestJS Reads Metadata
Build Dependency Graph
Instantiate Classes with Injected Dependencies
Run Application
This flow shows how NestJS uses decorators to mark classes and methods, reads metadata, builds dependencies, and runs the app.
Execution Sample
NestJS
import { Controller, Get } from '@nestjs/common';

@Controller('cats')
export class CatsController {
  @Get()
  findAll() { return 'All cats'; }
}
Defines a controller with a route using decorators to mark class and method for NestJS routing.
Execution Table
StepActionDecorator ProcessedMetadata AddedEffect
1Read @Controller('cats') on CatsController@ControllerRoute prefix 'cats' addedClass marked as controller with base route
2Read @Get() on findAll method@GetRoute GET '/' addedMethod marked as GET handler
3NestJS builds routing tableN/AController and routes registeredApp knows to call findAll on GET /cats
4App starts and listensN/AServer runningReady to handle requests
5Request GET /cats receivedN/ARoute matchedfindAll method called
6findAll returns 'All cats'N/AResponse generatedClient receives 'All cats'
7No more requestsN/AIdleWaiting for next request
💡 Execution stops when no more requests are received or app is stopped.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 5Final
CatsController Metadata{}{ routePrefix: 'cats' }{ routePrefix: 'cats', routes: [{ method: 'GET', path: '/' }] }{ registered: true }{ registered: true, lastCall: 'findAll' }{ registered: true, lastCall: 'findAll', response: 'All cats' }
Key Moments - 3 Insights
Why does NestJS use decorators instead of normal functions?
Decorators add metadata to classes and methods that NestJS reads later to build routing and dependency injection, as shown in steps 1 and 2 of the execution_table.
How does NestJS know which method to call for a GET request?
The @Get decorator adds route info to the method metadata (step 2), which NestJS uses to match incoming requests to methods (step 5).
What happens if no decorator is used on a class?
Without decorators, NestJS does not register the class as a controller or provider, so it won't be part of the app's routing or dependency graph.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what metadata is added after step 2?
AServer starts listening
BRoute prefix 'cats' added to CatsController
CRoute GET '/' added to findAll method
DResponse 'All cats' generated
💡 Hint
Check the 'Metadata Added' column in row 2 of execution_table
At which step does NestJS register the controller and routes?
AStep 3
BStep 1
CStep 5
DStep 7
💡 Hint
Look for 'registered' status in the 'Effect' column in execution_table
If the @Get decorator is removed, what changes in the execution_table?
AStep 1 would fail to add route prefix
BStep 2 metadata would be missing, no GET route registered
CApp would not start at step 4
DResponse would still be 'All cats'
💡 Hint
Refer to step 2 where @Get adds route metadata
Concept Snapshot
Decorator-based architecture in NestJS:
- Use decorators like @Controller and @Get to mark classes and methods
- Decorators add metadata NestJS reads to build routing and DI
- NestJS creates instances and routes requests based on metadata
- This pattern keeps code clean and declarative
- Removing decorators means NestJS ignores those classes/methods
Full Transcript
In NestJS, decorators are special markers on classes and methods. When the app starts, NestJS reads these decorators to understand which classes are controllers and which methods handle routes. For example, @Controller('cats') marks a class as a controller with a base route 'cats'. The @Get decorator on a method marks it as a handler for GET requests. NestJS collects this metadata, builds a routing table, and when a request comes in, it calls the right method. This flow lets developers write clean code while NestJS handles wiring everything together.