NestJS

민태영·2023년 10월 6일
0

NestJS란?

  • 효율적이고 확장가능한 Node.js 서버 측 애플리케이션을 구축하기위한 프레임워크이다.
  • 프로그레시브 JavaScript를 사용하며 TypeScript로 빌드되고 완벽하게 지원한다.
  • OOP(Object Oriented Programming)
  • FP(Functional Programming)
  • FRP(Functional Reactive Programming)
  • 경량화 된 프레임워크인 Express의 단점을 보완하기 다양한 기능과 플러그인을 내장

NestJS CLI설치

$ npm i -g @nestjs/cli

잘 설치가 되면 아래의 아래와 같은 결과가 나온다.

$ nest
nest
Usage: nest <command> [options]

Options:
  -v, --version                                   Output the current version.
  -h, --help                                      Output usage information.

Commands:
  new|n [options] [name]                          Generate Nest application.
  build [options] [app]                           Build Nest application.
  start [options] [app]                           Run Nest application.
  info|i                                          Display Nest project details.
  add [options] <library>                         Adds support for an external library to your project.
  generate|g [options] <schematic> [name] [path]  Generate a Nest element.
    Schematics available on @nestjs/schematics collection:
      ┌───────────────┬─────────────┬──────────────────────────────────────────────┐
      │ name          │ alias       │ description                                  │
      │ application   │ application │ Generate a new application workspace         │
      │ class         │ cl          │ Generate a new class                         │
      │ configuration │ config      │ Generate a CLI configuration file            │
      │ controller    │ co          │ Generate a controller declaration            │
      │ decorator     │ d           │ Generate a custom decorator                  │
      │ filter        │ f           │ Generate a filter declaration                │
      │ gateway       │ ga          │ Generate a gateway declaration               │
      │ guard         │ gu          │ Generate a guard declaration                 │
      │ interceptor   │ itc         │ Generate an interceptor declaration          │
      │ interface     │ itf         │ Generate an interface                        │
      │ middleware    │ mi          │ Generate a middleware declaration            │
      │ module        │ mo          │ Generate a module declaration                │
      │ pipe          │ pi          │ Generate a pipe declaration                  │
      │ provider      │ pr          │ Generate a provider declaration              │
      │ resolver      │ r           │ Generate a GraphQL resolver declaration      │
      │ service       │ s           │ Generate a service declaration               │
      │ library       │ lib         │ Generate a new library within a monorepo     │
      │ sub-app       │ app         │ Generate a new application within a monorepo │
      │ resource      │ res         │ Generate a new CRUD resource                 │
      └───────────────┴─────────────┴──────────────────────────────────────────────┘

새로운 프로젝트생성명령어

$ nest new project-name

nest프로젝트의 기본구조

1) main.ts

import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();
  • bootstrap을 호출
  • bootstrap함수 안에서 AppModule을 불러와서 NestFactory가 애플리케이션 객체를 생성 후 3000포트로 HTTP요청을 받음

2) app.module.ts

import { Module } from "@nestjs/common";
import { AppController } from "./app.controller";
import { AppService } from "./app.service";

@Module({
  imports: [],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

하나의 NestJS애플리케이션은 보통 여러개의 모듈로 이루어지는데 모듈은 서로 의존할 수 있다.
imports속성에 이 의존관계를 명시하도록 되어있다.

  • @Module() 데코레이터는 imports, controllers, providers 속성으로 이루어진 객체를 인자로 받는다.
  • controllers 속성에는 HTTP 요청을 받아서 응답을 보내는 컨트롤러 클래스를 나열
  • providers 속성에는 컨트롤러가 사용하는 다양한 일반 클래스(주로 서비스 클래스)를 나열
  • imports 속성에는 해당 모듈이 의존하고 있는 다른 모듈을 나열
profile
꿈을 꾸는 개발자

0개의 댓글