NestJS 11 · Controllers, providers and modules

Write a controller

Decorators map methods to routes. Pipes such as ParseIntPipe convert and validate parameters.

@Controller('cats')
export class CatsController {
  constructor(private readonly cats: CatsService) {}

  @Get()
  findAll(@Query('limit') limit?: string) {
    return this.cats.findAll(limit ? +limit : undefined);
  }

  @Get(':id')
  findOne(@Param('id', ParseIntPipe) id: number) {
    return this.cats.findOne(id);
  }

  @Post()
  @HttpCode(201)
  create(@Body() dto: CreateCatDto) {
    return this.cats.create(dto);
  }
}

Official NestJS 11 documentation

More controllers, providers and modules commands

All controllers, providers and modules commands · NestJS 11 cheatsheet

Was this page helpful?