[NestJS] NestJS 공부
목차
NestJS - A progressive Node.js framework
NestJS is a framework for building efficient, scalable Node.js web applications. It uses modern JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Progamming), FP (Functional Programming), and FRP (Functional Reactive Progra
nestjs.com
NestJS 개요
NestJS는 효율적이고 확장 가능한 Node.js 서버 측 application을 빌드하기 위한 프레임워크이다. progressive JavaScript를 사옹하고 TypeScript 지원하며 OOP(Object Oriented Programming), FP(Functional Programming) 및 FRP(Functional Reactive Programming) 요소를 결합한다.
또한 Springboot와 유사한 점이 많은 프레임워크이다.
내부적으로 Nest는 Express와 같은 강력한 HTTP서버 프레임워크를 사용하며 선택적으로 Fastify를 사용하도록 구성할 수 있다.
Nest는 Node.js 프레임워크 (Express/Fastify)의 API를 직접 사용할 수도 있다. 이를 통해 개발자는 기본 플랫폼에서 사용할 수 있는 수 많은 타사의 모듈을 자유롭게 사용할 수 있다. 또한 F/E코드와 B/E코드를 하나의 언어로 작성할 수 있다는 장점이 있다.
NestJS project 구조는 크게 세가지로 나눠서 본다.
- Controllers
- Providers (=Service)
- Modules
Controllers
Spring에서 쓰는 Controller와 동일함. 들어오는 Request를 핸들링하고 Response를 Client에 돌려준다.
기본 Controller 만들기
cats.controller.ts
@Controller 데코레이터를 사용한다. (java의 annotation 같은거)
cats의 선택적 경로 prefix를 지정할 수 있다. @Controller() 데코레이터의 prefix 경로를 사용함으로써 관련된 라우트들을 쉽게 그룹화할 수 있고 코드의 반복을 최소화할 수 있다. 예를들어 /customers 경로 아래에 고객 entity와 상호 작용을 관리하는 경로의 집합을 그룹화 하도록 할 수 있다. customers prefix를 지정하여 각 경로의 해당 부분을 반복 할 필요가 없도록 한다.
import { Controller, Get } from '@nestjs/common';
@Controller('cats')
export class CatsController {
@Get()
findAll(): string {
return 'This action returns all cats';
}
}