0
0
NestJSframework~10 mins

Controller decorator in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Controller decorator
Define Controller Class
Apply @Controller Decorator
NestJS Registers Route Prefix
Define Route Handlers inside Class
Handle Incoming HTTP Requests
Execute Handler Method
Send Response to Client
The @Controller decorator marks a class as a controller, registering it with NestJS to handle HTTP requests for a specific route prefix.
Execution Sample
NestJS
import { Controller, Get } from '@nestjs/common';

@Controller('cats')
export class CatsController {
  @Get()
  findAll() { return 'All cats'; }
}
Defines a controller for the 'cats' route that returns 'All cats' on GET requests.
Execution Table
StepActionEvaluationResult
1NestJS reads @Controller('cats') decoratorRegisters 'cats' as route prefixCatsController linked to '/cats' route
2NestJS reads @Get() decorator on findAll()Registers GET handler for '/cats'GET requests to '/cats' call findAll()
3Incoming HTTP GET request to '/cats'Matches registered route and methodCalls findAll() method
4findAll() executesReturns string 'All cats'Response body set to 'All cats'
5Response sent to clientClient receives 'All cats'Request cycle complete
💡 Request handled and response sent; no further action
Variable Tracker
VariableStartAfter Step 3After Step 4Final
routePrefixundefined'cats''cats''cats'
handlerMethodundefinedfindAll()findAll()findAll()
responseBodyundefinedundefined'All cats''All cats'
Key Moments - 3 Insights
Why does the @Controller decorator take a string like 'cats'?
The string defines the route prefix for all handlers in the class, as shown in execution_table step 1 where 'cats' is registered as the route prefix.
How does NestJS know which method handles GET requests?
The @Get() decorator marks the method as a GET handler, registered in step 2 of the execution_table.
What happens if a request comes to a different route?
NestJS matches routes by prefix and method; if no match is found, the request is not handled by this controller, as implied by step 3 where only '/cats' GET requests call findAll().
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the route prefix registered at step 1?
A'GET'
B'findAll'
C'cats'
D'@Controller'
💡 Hint
Check the 'Evaluation' column in step 1 of the execution_table.
At which step does the findAll() method execute?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for the step where the method returns 'All cats' in the execution_table.
If the @Controller decorator was changed to @Controller('dogs'), what changes in the variable_tracker?
AhandlerMethod changes to 'dogs'
BroutePrefix changes to 'dogs'
CresponseBody changes to 'dogs'
DNo changes
💡 Hint
Refer to the 'routePrefix' variable in the variable_tracker.
Concept Snapshot
@Controller('route') decorator marks a class as a controller.
It sets the route prefix for all handlers inside.
Methods decorated with @Get(), @Post(), etc., handle HTTP requests.
NestJS uses these to route requests to the right method.
Handlers return responses sent back to clients.
Full Transcript
The Controller decorator in NestJS marks a class as a controller that handles HTTP requests. When you add @Controller('cats'), NestJS registers the class to handle routes starting with '/cats'. Inside the class, methods decorated with @Get(), @Post(), and others define handlers for specific HTTP methods. For example, @Get() on findAll() means GET requests to '/cats' call findAll(). When a request comes in, NestJS matches the route and method, calls the handler, and sends back the response. This flow helps organize your server code by grouping related routes and handlers in classes.