Web/NestJS

Controllers

verbena 2024. 2. 15.

Controllers

Controllers are responsible for handling incoming requests and returning responses to the client.

A controller's purpose is to receive specific requests for the application. The routing mechanism controls which controller receives which requests. Frequently, each controller has more than one route, and different routes can perform different actions.

In order to create a basic controller, we use classes and decorators. Decorators associate classes with required metadata and enable Nest to create a routing map (tie requests to the corresponding controllers).

// For quickly creating a CRUD controller with the validation built-in,
// you may use the CLI's CRUD generator

nest g resource [name]
컨트롤러는 들어오는 요청을 받고 처리된 결과를 응답으로 돌려주는 인터페이스 역할을 한다. 

Request payload

A DTO(Data Transfer Object) is an object that defines how the data will be sent over the network.We could determine the DTO schema by using TypeScript interfaces, or by simple classes.Interestingly, we recommend using classes here.

Why? Classes are part of the JavaScript ES6 standard, and therefore they are preserved as real entities in the compiled JavaScript. On the other hand, since TypeScript interfaces are removed during the transpilation, 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.

ValidationPipe can filter out properies that should not be received by the method handler. In this case, we can whitelist the acceptable properties, and any property not included in the whitelist is automatically stripped from the resulting object.

'Web > NestJS' 카테고리의 다른 글

Module  (0) 2024.02.22
Provider  (0) 2024.02.20
Exploring EcmaScript Decorators  (0) 2024.02.15

댓글