Nest.js

Nest.js - ๊ฐœ๋… & ์ดˆ๊ธฐ์…‹ํŒ…

martinooo 2022. 12. 1. 12:31

๐Ÿคณ Nest.js ๋ž€?


  • Nest.js๋Š” ํšจ์œจ์ ์ด๊ณ  ํ™•์žฅ ๊ฐ€๋Šฅํ•œ Node.js ์„œ๋ฒ„ ์ธก ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ตฌ์ถ•ํ•˜๊ธฐ ์œ„ํ•œ ํ”„๋ ˆ์ž„์›Œํฌ์ด๋‹ค. 
  • JavaScript๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  TypeScript๋กœ ๊ตฌ์ถ•๋˜์–ด ์™„๋ฒฝํ•˜๊ฒŒ ์ง€์›ํ•˜๋ฉฐ OOP, FP, FRP ์š”์†Œ๋ฅผ ๊ฒฐํ•ฉํ•ฉ๋‹ˆ๋‹ค. 
    • OOP(Object Oriented Programming)
    • FP(Functional Programming)
    • FRP(Functional Reactive Programming)
  • ๋‚ด๋ถ€์ ์œผ๋กœ Nest.js๋Š” Express์™€ ๊ฐ™์€ ๊ฐ•๋ ฅํ•œ Http ์„œ๋ฒ„ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ ์„ ํƒ์ ์œผ๋กœ Fastify๋„ ์‚ฌ์šฉํ•˜๋„๋ก ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. 

 


๐Ÿ‘€ Nest.js ์žฅ์ 


Nest.js๋Š” Express๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ์›น ํ”„๋ ˆ์ž„์›Œํฌ๋‹ค. Java์˜ Spring์™€ ๋น„์Šทํ•œ ์•„ํ‚คํ…์ณ ๊ตฌ์กฐ๋ฅผ ์ œ๊ณตํ•˜๋ฉฐ, ๋ผ์šฐํŒ…, ๋ณด์•ˆ๊ณผ ๊ด€๋ จํ•˜์—ฌ ๋‹ค์–‘ํ•œ ์ข‹์€ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์›น๊ฐœ๋ฐœ์— ์ƒ์‚ฐ์„ฑ์„ ๋†’์—ฌ ์ค€๋‹ค.


๐Ÿ‘€ Nest.js ํ”„๋กœ์ ํŠธ ์…‹ํŒ… 1


$ npm i -g @nestjs/cli
$ nest new project-name 
ํ˜„์žฌ ํด๋”์— ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ์‹œ nest new .

์„ค์น˜์‹œ ํŒจํ‚ค์ง€ ์„ค์ •: npm

ํ„ฐ๋ฏธ๋„์— nest ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด nest ์ œ๊ณตํ•˜๋Š” ๋ชจ๋“ˆ์„ ํ•œ๋ฒˆ์— ๋ณผ์ˆ˜์žˆ๋‹ค.

nest generate co //๋ช…๋ น์–ด๋กœ module์„ ์…‹ํŒ…ํ•  ์ˆ˜ ์žˆ๋‹ค. 
์ค„์—ฌ์„œ nest g co

 

Nest CLI ์„ค์น˜์— ๋Œ€ํ•œ ๋” ๋งŽ์€ ์ •๋ณด ์•„๋ž˜์— ์‚ฌ์ดํŠธ์—์„œ ํ™•์ธํ• ์ˆ˜์žˆ๋‹ค.

https://docs.nestjs.com/cli/overview

 

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


๐Ÿ‘€ Nest.js ํ”„๋กœ์ ํŠธ ์…‹ํŒ… 2


1. ํ”„๋กœ์ ํŠธ ์„ค์น˜ ์ดํ›„ ์•„๋ž˜์— ํด๋” ๋ฐ ํŒŒ์ผ๋“ค์ด ์ƒ์„ฑ๋œ๋‹ค.

2. src/main.ts (env ํ™˜๊ฒฝ ์„ค์ • ๋ฐ ํฌํŠธ ๋ฒˆํ˜ธ ์„ค์ •)

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

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const port = process.env.PORT || 3000;
  await app.listen(port);
}
bootstrap();

3. ESLint & Prettier ์ฝ”๋“œ ์ปจ๋ฒค์…˜ ์„ค์ •


ESLint ์™€ Prettier์— ๋Œ€ํ•œ ๋” ๋งŽ์€ ์ •๋ณด๋ฅผ ์•„๋ž˜์˜ ์‚ฌ์ดํŠธ์—์„œ ํ™•์ธํ• ์ˆ˜์žˆ๋‹ค.

https://cometruedream.tistory.com/131

 

ESLint, Prettier Setting ์ดํ•ดํ•˜๋ฉฐ ์ •๋ฆฌ

๐Ÿคณ ESLint & Prettier ์ •ํ™•ํ•œ ์ฐจ์ด? ESLint ์™€ Prettier๋Š” ๋‘˜๋‹ค ์ฝ”๋“œ ์ปจ๋ฒค์…˜์„ ๋„์™€์ฃผ๋Š” ์—ญํ™œ์„ ํ•œ๋‹ค. ๋‘˜์ค‘์— ํ•˜๋‚˜๋งŒ ์จ๋„ ๋˜๋Š”๋ฐ ์™œ ๋‘˜์„ ๊ฐ™์ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค. ESLint ์™€ Prettier ๋‹ค๋ฅธ ์ฐจ์ด๊ฐ€ ์žˆ๊ธฐ ๋•Œ

cometruedream.tistory.com


  • vscode ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ctrl + shift + p๋กœ Preferences: Workspace Settings(JSON) ์—ด์–ด ๋‹ค์Œ์„ ์ถ”๊ฐ€
{
    "editor.defaultFormatter": "esbenp.prettier-vscode",
    "eslint.alwaysShowStatus": true,
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
      "source.fixAll.eslint": true
    },
  }
  • ํ•ด๋‹น ํ”„๋กœ์ ํŠธ ํด๋”์— .prettierrc ์„ค์ •์— ์ถ”๊ฐ€
# .prettierrc

{
  "printWidth": 150,
  "tabWidth": 2,
  "singleQuote": true,
  "trailingComma": "all",
  "bracketSpacing": true,
  "semi": true,
  "arrowParens": "always",
  "endOfLine": "lf",
  "proseWrap": "preserve"
}

4. ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์„ค์ •

  • ์ผ๋ฐ˜์ ์œผ๋กœ node.js ํ™˜๊ฒฝ์—์„  ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ํ™˜๊ฒฝ(production, development) ๋ณ„ ์ƒ์ดํ•œ ๋ณ€์ˆ˜ ๊ฐ’์„ .env ํŒŒ์ผ์„ ํ™œ์šฉํ•˜์—ฌ ์„ค์ •ํ•œ๋‹ค. nest.js๋„ .env ์„ค์ •์ด ๊ฐ€๋Šฅํ•œ๋ฐ ์ฐจ์ด์ ์€ ConfigModule๋ฅผ ํ™œ์šฉํ•œ๋‹ค.
  • ๊ด€๋ จ ํŒจํ‚ค์ง€ ์„ค์น˜: npm i --save @nestjs/config 
  • app.module.ts ์—์„œ ์‚ฌ์šฉํ•  ๋ชจ๋“ˆ import 
  • appmodule ์—์„œ ์‚ฌ์šฉํ•  ๋ชจ๋“ˆ ๊ฐ€์ ธ์™€์„œ imports ์„ค์ • 
    • {isGLobal: true} : ConfigModule๋ฅผ AppModule ์ด์™ธ์˜ ๋ชจ๋“ˆ์—์„œ ๋ฐ˜๋ณต importํ•  ํ•„์š”์—†์ด ์ „์—ญ ๋ชจ๋“ˆ๋กœ ์„ค์ •ํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
    }),
  ],
  controllers: [],
  providers: [],
})
export class AppModule {}

4. ๊ธฐ์ดˆ ํ™˜๊ฒฝ์„ค์ • ์…‹ํŒ…ํ›„ ํ…Œ์ŠคํŠธ

npm run start:dev //๋ช…๋ น์–ด ์ž…๋ ฅ

์„ฑ๊ณต์ ์œผ๋กœ ์„œ๋ฒ„๋ฅผ ๋„์—ˆ๋‹ค.

 

๋‹ค์Œ์—๋Š” ๋‹ค์–‘ํ•œ ๋ชจ๋“ˆ ์„ค์ •ํ›„ ๋ณด์•ˆ์„ค์ •๋“ฑ์„ ํ•ด๋ณผ ์˜ˆ์ •์ด๋‹ค.