What Are Controllers in Nest.js
Deslize para mostrar o menu
Controllers are responsible for handling incoming requests and returning responses.
When a client sends a request to your server, the controller receives it and decides what should happen next.
In Nest.js, controllers are defined using decorators. A decorator tells Nest how a class or method should behave.
For example:
import { Controller, Get } from '@nestjs/common';
@Controller('users')
export class UsersController {
@Get()
getAllUsers() {
return 'List of users';
}
}
In this example:
@Controller('users'): defines the base route;@Get(): handles GET requests;getAllUsers(): returns a response.
When you open /users in the browser, this method is executed.
Controllers should stay simple. Their job is to:
- Receive the request;
- Call the appropriate logic;
- Return a response.
They should not contain complex logic. That logic belongs in services.
Tudo estava claro?
Obrigado pelo seu feedback!
Seção 2. Capítulo 1
Pergunte à IA
Pergunte à IA
Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo
Seção 2. Capítulo 1