Performance: Controller decorator
This affects the routing setup and initial request handling speed in a NestJS application.
import { Controller, Get } from '@nestjs/common'; @Controller('api') export class AppController { @Get('users') getUsers() { // logic } @Get('products') getProducts() { // logic } @Get('orders') getOrders() { // logic } }
import { Controller, Get } from '@nestjs/common'; @Controller('') export class AppController { @Get('users') getUsers() { // logic } @Get('products') getProducts() { // logic } @Get('orders') getOrders() { // logic } }
| Pattern | Routing Checks | Request Latency | Code Complexity | Verdict |
|---|---|---|---|---|
| Empty string prefix in @Controller | High (all routes at root) | Higher latency due to more checks | Moderate | [X] Bad |
| Specific prefix in @Controller (e.g., 'api') | Lower (grouped routes) | Lower latency with faster matching | Moderate | [OK] Good |
@Controller decorator in NestJS?@Controller decorator marks a class as a handler for HTTP requests grouped under a base path.@Controller decorator takes the base path as a string argument inside parentheses.@Controller('users'). Others misuse dot, equal sign, or invalid syntax.getAllUsers method?
@Controller('api')
export class UserController {
@Get('users')
getAllUsers() {
return 'List of users';
}
}@Controller('api').@Get('users').@Controller('products')
export class ProductController {
@Get()
getAll() {
return 'All products';
}
@Get('')
getEmpty() {
return 'Empty route';
}
}@Controller decorator to achieve this?@Controller decorator accepts a single string as the base path. It does not support multiple paths in an array.Pass the test and this pattern moves unknown