Jay's Cookbook
Menu
  • Tags
  • Categories
  • Projects
Computer Science
OS
Network
Data Structure
Algorithm
Language
Code Architecture
Python
Javascript
Typescript
Java
Backend
Backend Theory
TypeORM
Node.js
NestJS
FastAPI
Frontend
HTML/CSS
React
Next.js
Data Engineering
DE Theory
MySQL
MongoDB
Elastic
Redis
Kafka
Spark
Airflow
AI
Basic
Pytorch
NLP
Computer Vision
Data Analytics
Statistics
Pandas
Matplotlib
DevOps
Git
Docker
Kubernetes
AWS
[NestJS] Intro
backend
NestJS

[NestJS] Intro

Jay Kim
Jay Kim 01 Dec 2023
시작 [NestJS] 구성요소(1) Modules, Controllers

Table of Contents

  • NestJS란
  • Express와 비교
  • NestJS 시작하기
  • NestJS 코드 구조
  • NestJS Request Lifecycle

NestJS란

  • node.js 런타임 위에서 동작하는 백엔드 프레임워크
  • HTTP 요청과 같은 기본적인 기능은 디폴트로 Express를 사용하고 있음 (Fastify로 교체 가능)

Express와 비교

  • Express를 래핑한 NestJS를 통해 얻게 되는 이점은 다음과 같다
    • 구조화되어 있는 아키텍처
    • Typescript 친화적
    • 강력한 CLI 제공
    • OOP 개발의 핵심 개념인 제어의 역전을 위해 필요한 의존성 주입 기능을 제공

NestJS 시작하기

  • Nest CLI 사용하면 쉽게 템플릿 코드 생성할 수 있음

  • Nest CLI 설치

npm i -g @nestjs/cli
  • 템플릿 코드 생성
nest new .
  • 개발 서버 실행
npm run start:dev

NestJS 코드 구조

  • NestJS는 코드를 모듈화 함으로써 재사용성을 높임
  • 시작점이 되는 루트 모듈과, 각각의 기능을 분리시켜 놓은 모듈로 이루어짐

  • NestJS는 크게 다음과 같은 요소들로 이루어짐
    • Module: 모듈화 하는 역할
    • Controller: 요청을 받고 응답을 돌려주는 역할
    • Service: 비즈니스 로직을 수행하는 역할
    • Repository: DB와 관련된 작업을 수행하는 역할
    • DTO: 데이터를 검증하고, 변환하는 역할
    • Middleware: 요청과 관련된 공통적으로 자주 사용되는 작업을 수행하는 역할 (NestJS에서는 Middleware보다는 Guard, Interceptor, Pipe 사용 권장)
    • Guard: 인증/인가와 관련된 작업을 수행하는 역할
    • Interceptor: Middleware와 역할이 비슷하지만, 다른 점은 Interceptor는 요청 뿐만 아니라, 응답에 대해서도 가능
    • Pipe: 데이터가 Controller에게 전달되기 전에 검증/변환하는 역할
    • Filter: 예외를 처리하는 역할

NestJS Request Lifecycle

  • NestJS 백엔드 어플리케이션 안에서 요청(Request) 객체는 다음과 같은 순서로 순회한다
    • Middleware -> Guard -> Interceptor -> Pipe -> Controller -> Service
    • Guard, Interceptor, Pipe에 대해 글로벌, 컨트롤러, 핸들러 스코프 순서로 순회
  • Service에서 반환한 응답(Response) 객체는 다음과 같은 순서를 순회하고 유저에게 반환된다
    • Service -> Controller -> Interceptor -> Filter
    • Interceptor, Filter에 대해 핸들러, 컨트롤러, 글로벌 스코프 순서로 순회
시작 [NestJS] 구성요소(1) Modules, Controllers

You may also like

See all NestJS
14 Oct 2024 [NestJS] API 문서화
backend
NestJS

[NestJS] API 문서화

09 Oct 2024 [NestJS] SSE
backend
NestJS

[NestJS] SSE

08 Oct 2024 [NestJS] 웹 소켓
backend
NestJS

[NestJS] 웹 소켓

Jay Kim

Jay Kim

Web development, data engineering for human for the Earth. I share posts, free resources and inspiration.

Rest
Lifestyle
Hobby
Hobby
Hobby
Hobby
2025 © Jay's Cookbook. Crafted & Designed by Artem Sheludko.