# Controllers & Routing — NestJS: Building Scalable Node.js APIs

Source: https://www.skillbyai.com/en/nestjs/nestjs-controllers

> Handle HTTP requests with @Controller and route decorators such as @Get and @Post, and read params and queries.

## A basic controller

`@Controller` marks a class as a controller; `@Get()`, `@Post()`, etc. decorate handler methods.

```typescript
import { Controller, Get, Post, Body } from '@nestjs/common';
import { UsersService } from './users.service';

@Controller('users')
export class UsersController {
  constructor(private usersService: UsersService) {}

  @Get()
  findAll() {
    return this.usersService.findAll();
  }

  @Post()
  create(@Body() createUserDto: CreateUserDto) {
    return this.usersService.create(createUserDto);
  }
}
```

## Route params & query

Use `@Param()` for URL segments, `@Query()` for query strings.

```typescript
@Get(':id')
findOne(@Param('id') id: string) {
  return this.usersService.findOne(id);
}

@Get()
findAll(@Query('limit') limit: number = 10) {
  return this.usersService.findAll(limit);
}
```

## HTTP verb decorators

`@Get`, `@Post`, `@Put`, `@Delete`, `@Patch` map to HTTP methods; the path argument is relative to the controller's base path.

## Keep controllers thin

A controller should only read the request and call a service. Business rules inside a controller cannot be reused by a CLI or a queue worker, but a service can.
