The Controller decorator marks a class as a controller that handles incoming requests and sends responses in a NestJS app.
Controller decorator in NestJS
@Controller('route-path') export class SomeController { // handler methods here }
The string inside @Controller() sets the base route path for all handlers in the class.
If you leave it empty like @Controller(), it will handle requests at the root path.
/users.@Controller('users') export class UsersController { // handles routes starting with /users }
/.@Controller()
export class AppController {
// handles routes at the root path /
}/products.@Controller('products') export class ProductsController { // add methods to handle product routes }
This example creates a controller with the base route /hello. It has one GET handler that returns a greeting string.
import { Controller, Get } from '@nestjs/common'; @Controller('hello') export class HelloController { @Get() sayHello() { return 'Hello, NestJS!'; } }
Controllers should be simple and only handle routing and request/response logic.
Use decorators like @Get(), @Post() inside controllers to define specific routes.
Controllers are automatically discovered if registered in a module.
@Controller marks a class as a request handler group in NestJS.
It sets a base path for all routes inside the class.
Use it to organize your app's routes clearly and cleanly.
Practice
@Controller decorator in NestJS?Solution
Step 1: Understand the role of @Controller
The@Controllerdecorator marks a class as a handler for HTTP requests grouped under a base path.Step 2: Differentiate from other decorators
It is not used for services, database, or styling but specifically for routing.Final Answer:
To mark a class as a group of routes with a base path -> Option BQuick Check:
@Controller = route group [OK]
- Confusing @Controller with @Injectable
- Thinking it styles components
- Assuming it manages database connections
Solution
Step 1: Recall correct decorator syntax
The@Controllerdecorator takes the base path as a string argument inside parentheses.Step 2: Check each option's syntax
Only @Controller('users') export class UsersController {} uses the correct syntax:@Controller('users'). Others misuse dot, equal sign, or invalid syntax.Final Answer:
@Controller('users') export class UsersController {} -> Option CQuick Check:
@Controller('path') syntax = @Controller('users') export class UsersController {} [OK]
- Using dot notation instead of parentheses
- Assigning with equal sign instead of decorator call
- Writing invalid decorator syntax
getAllUsers method?
@Controller('api')
export class UserController {
@Get('users')
getAllUsers() {
return 'List of users';
}
}Solution
Step 1: Identify base path from @Controller
The base path is 'api' from@Controller('api').Step 2: Identify route path from @Get decorator
The method route is 'users' from@Get('users').Step 3: Combine base and method paths
Full path is base + method = '/api/users'.Final Answer:
/api/users -> Option DQuick Check:
Base + method path = /api/users [OK]
- Reversing base and method paths
- Using method name as URL path
- Ignoring base path in URL
@Controller('products')
export class ProductController {
@Get()
getAll() {
return 'All products';
}
@Get('')
getEmpty() {
return 'Empty route';
}
}Solution
Step 1: Analyze route paths from @Get decorators
Both methods respond to the same route: base 'products' + '' or no path means '/products'.Step 2: Understand route conflict
Having two methods for the same route causes a conflict and runtime error.Final Answer:
Duplicate routes for the same path cause a conflict -> Option AQuick Check:
Duplicate routes = conflict error [OK]
- Focusing on @Get parentheses instead of duplicate routes
- Believing controller path needs leading slash
- Assuming string return type is invalid
@Controller decorator to achieve this?Solution
Step 1: Understand @Controller supports single base path
The@Controllerdecorator accepts a single string as the base path. It does not support multiple paths in an array.Step 2: Evaluate options for multiple paths
Create two separate controllers, one for 'admin' and one for 'user' is correct because creating two separate controllers is the proper way to handle multiple base paths. @Controller(['admin', 'user']) export class MultiPathController {} is invalid syntax. @Controller('admin,user') export class MultiPathController {} uses a comma-separated string which is treated as a single path. @Controller('admin') @Controller('user') export class MultiPathController {} uses multiple decorators which is invalid.Final Answer:
Create two separate controllers, one for 'admin' and one for 'user' -> Option AQuick Check:
One controller per base path [OK]
- Using array of strings in @Controller (not supported)
- Applying multiple @Controller decorators on one class
- Trying to combine multiple paths in one string
Ready to prove it?
Pass the test and this pattern moves unknown
