WebDevLee 2023. 8. 19. 16:18

Nest의 기본 개념에 대해 정리하였습니다.



< Introduction  >

Nest.js는 Express위에서 동작하는 node.js 프레임워크 입니다.

TypeScript를 사용합니다.(그러나 개발자가 바닐라 자바스크립트로 개발 가능)

기존 Node.js + Express의 장점이자 단점인 자율성(API url이나 파일구조 등)을 보완한 프레임워크로, 구조 및 규칙이 정해져있습니다. 하지만 그 구조 및 규칙을 따르기만 한다면, 큰 규모의 백엔드를 쉽게 만들 수 있습니다.

OOP(Object Oriented Programming), FP(Functional Programming), FRP(Functional Reactive Programming)을 특징으로 가지고 있습니다.

 

 


< Project Setup  >

1. Nest CLI 설치

$ npm i -g @nestjs/cli

2. 프로젝트 시작

$ nest new project-name

 

https://docs.nestjs.com/first-steps

 

Documentation | NestJS - A progressive Node.js framework

Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Progamming), FP (Functional Programming), and FRP (Functional Reac

docs.nestjs.com

 

 


< Architecture  >

코어 파일들은 /src에 들어있습니다.

main.ts 파일을 통해 서버가 시작됩니다.(entry file)

 

  • main.ts The entry file of the application which uses the core function NestFactory to create a Nest application instance.
  • app.module.ts : The root module of the application.
  • app.controller.ts : A basic controller with a single route.
  • app.service.ts : A basic service with a single method.
  • app.controller.spec.ts : The unit tests for the controller(optional).

 

+. 모듈은 어플리케이션의 일부분, 작은 어플리케이션입니다.
ex) 인증을 담당하는 어플리케이션 => users 모듈

 


Controller : 

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

Providers : 

  • Providers are a fundamental concept in Nest.
  • Many of the basic Nest classes may be treated as a provider – services, repositories, factories, helpers, and so on.
  • The main idea of a provider is that it can be injected as a dependency; this means objects can create various relationships with each other, and the function of "wiring up" instances of objects can largely be delegated to the Nest runtime system.

 

Services : 

  • Services are responsible for business logic(the part the has actual functions).

 

+. Nest.js는 디폴트로 json 형식을 반환합니다.

+. Nest.js는 디폴트로 Express위에서 돌아가지만, fastify 프레임워크 위에서 돌아가도록 할 수 있습니다.(Express보다 2배 빠름)
    -> Req, Res같은 decorator를 이용해 Express에 접근 가능하지만, fastify 프레임워크로 바꾸려 할 때에는 좋지 않은 방식임