-
[Nest.js] 3 - ControllerNest.js 2021. 9. 8. 12:45
Contents
1. Controller?
2. Route?
3. Request
4. Response
5. Create Controller
1. Controller?
- Controllers are responsible for handling incoming requests and returning responses to the client.
https://docs.nestjs.com/controllers#controllers - A Controller's purpose is to receive specific requests for the application.
2. Routing?
- The Routing mechanism controls which controller receives which requests.
- Each Controller has more than one route, and different routes can perform different actions.
3. Request
- @Controller() decorator, which is required to define a basic controller.
We put a path prefix in a @Controller('path') decorator allows to group a set of related routes,
, and minimize repetitive code.- CRUD Example with HTTP Requests and Handlers
import { Controller, Get, Post, Patch, Delete } from '@nestjs/common'; @Controller('users') // requests starting with /users export class CatsController { constructor(private readonly usersService: UsersService) {} @Post() // POST /users create(@Body() createUserDto: CreateUserDto) { return this.usersService.create(createUserDto); } @Get() // GET /users findAll() { return this.usersService.findAll(); } @Get(':id') // GET /users/id findOne(@Param('id') id: string) { return this.usersService.findOne(+id); } @Patch(':id') // PATCH /users/id update(@Param('id') id: string, @Body() updateUserDto: UpdateUserDto) { return this.usersService.update(+id, updateUserDto); } @Delete(':id') // DELETE /users/id remove(@Param('id') id: string) { return this.usersService.remove(+id); } }
- DTO(Data Transfer Object) : created by TypeScript Interface or by Class
#RECOMMEND USING CLASS
-> Classes are part of JS ES6 Standard, and therefore they are preserved as real entities in the complied JS.
-> TS Interfaces are removed duing the transpilation(Translate + Compilation).
-> Nest can't refer to them at runtime.
-> This is important because features such as Pipes enable additional possibilities
when they have access to the metatype of the variable at runtime.- @Body() xxxDTO
-> it would create a xxxDTO with user's input data by matching xxxDTO's properties and names.- @Param() params
-> params.id- @Param('id) id: string
-> id
4. Response
- A Request Handler returns a Javascript Object or Array, it will automatically be serialized to JSON.
- Primitive type(e.g., string, number, boolean) will be sent just value without attempting to serialize it.
- Simple : just return the value, and Nest takes care of the rest
- 200 Status code is default, 201 Status code is for POST success.
- @HttpCode(...) decorator at handler-level can change the default status code.
5. Create Controller using CLI
1. CLI
$ nest g controller boards // "boards" is the name of controller, // create BoardsController or $ nest g controller boards --no-spec // --no-spec optio : exclude unit-test file
2. Look for a "boards" folder
3. Created "boards.controller.ts" in the folder
4. Imported and Registerd the controller in its module "boards.module.ts" automatically
'Nest.js' 카테고리의 다른 글
[Nest.js] 5 - TypeORM / MySql(MariaDB) (0) 2021.09.08 [Nest.js] 4 - Provider (0) 2021.09.08 [Nest.js] 2 - Module (0) 2021.09.07 [Nest.js] 1 - Basic Structure (0) 2021.09.07 [Nest.js] 0 - Nest.js ? (0) 2021.09.07