@Controller()
Marks a class as a controller that handles incoming HTTP requests for a route prefix.
Syntax
@Controller(prefixOrOptions?: string | string[] | ControllerOptions) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
prefixOrOptions | string | string[] | ControllerOptions | No | Route path prefix, or an options object with path, host, scope, and version. |
Returns
ClassDecorator — A decorator applied to the controller class.
Examples
import { Controller, Get } from '@nestjs/common';
@Controller('cats')
export class CatsController {
@Get()
findAll(): string {
return 'This returns all cats';
}
}
import { Controller, Get } from '@nestjs/common';
@Controller({ path: 'users', version: '1' })
export class UsersController {
@Get()
list() {
return [];
}
}
Notes
Controllers are responsible for handling requests and returning responses.
The path prefix passed here is combined with the method-level route
decorators. Use the options object to set host-based routing, request
scope, or API versioning.