0
0
NestJSframework~5 mins

Controller decorator in NestJS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @Controller decorator in NestJS?
The <code>@Controller</code> decorator marks a class as a controller that can handle incoming HTTP requests and send responses. It groups related request handlers under a common route prefix.
Click to reveal answer
beginner
How do you specify a route prefix using the @Controller decorator?
You pass a string to @Controller('prefix'). This prefix is added before all routes defined in the controller's methods.
Click to reveal answer
beginner
Can a NestJS controller class have multiple route handlers? How?
Yes. Inside a controller class, you define multiple methods decorated with HTTP method decorators like @Get(), @Post(), etc. Each method handles a different route or HTTP verb.
Click to reveal answer
beginner
What happens if you use @Controller() without any prefix string?
The controller will handle routes starting from the root path /. Its methods define the full route paths.
Click to reveal answer
intermediate
How does NestJS use the @Controller decorator internally?
NestJS uses <code>@Controller</code> to register the class as a controller in its routing system. It reads the decorator metadata to map HTTP requests to the controller's methods.
Click to reveal answer
What does the @Controller('users') decorator do?
AImports the 'users' module
BSets the route prefix to '/users' for all routes in the controller
CCreates a database table called 'users'
DDefines a service named 'users'
Which decorator is used inside a controller to handle GET requests?
A@Get()
B@Post()
C@Put()
D@Delete()
If a controller has @Controller() with no prefix, what is the base route?
A/
B/controller
C/api
D/home
Can a single NestJS controller handle multiple HTTP methods?
AOnly if you use multiple <code>@Controller</code> decorators
BNo, one controller handles only one HTTP method
CYes, by defining multiple methods with different HTTP decorators
DOnly if you extend another controller
What is the main role of the @Controller decorator in NestJS?
ATo define a database schema
BTo configure the server port
CTo create a middleware
DTo register a class as a route handler
Explain how the @Controller decorator works in NestJS and how it helps organize routes.
Think about how a controller groups related web routes under one roof.
You got /4 concepts.
    Describe how you would create a NestJS controller that handles GET requests at '/products' and POST requests at '/products'.
    Focus on the decorator usage for the class and methods.
    You got /4 concepts.