๐Ÿ˜พnest.js ํ™˜๊ฒฝ์„ค์ •

yyยท2024๋…„ 1์›” 30์ผ
0

์žก๋™์‚ฐ์ด

๋ชฉ๋ก ๋ณด๊ธฐ
13/21
post-thumbnail

nest.js ์„ค์น˜ -> ํ”„๋กœ์ ํŠธ ํด๋” ์ƒ์„ฑ๊นŒ์ง€ ์™„๋ฃŒํ•œ ํ›„์˜ ํ™˜๊ฒฝ์„ค์ •

๊ธฐ๋ณธ ์ƒ์„ฑ๋œ ํŒŒ์ผ

ํŒŒ์ผ์˜ ์ฝ”๋“œ๋“ค์€ service -> controller -> module -> main ์œผ๋กœ ํ˜๋Ÿฌ๊ฐ„๋‹ค.

โœ”๏ธ main.ts

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

async function bootstrap() {
  const app = await NestFactory.create(AppModule); 
  // ใ„ดAppModule์ด๋ผ๋Š” ๋ชจ๋“ˆ์„ ๋ฃจํŠธ ๋ชจ๋“ˆ๋กœ ์‚ฌ์šฉํ•˜๋Š” Nest.js ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ด์ค˜!
  await app.listen(3000);
}
bootstrap();
  • Nest.js ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ง„์ž…์ .
  • NestFactory ํด๋ž˜์Šค์˜ create๋ผ๋Š” ์ •์  ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด Nest.js ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒˆ๋กญ๊ฒŒ ์ƒ์„ฑ.

โœ”๏ธ 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 {}
  • @Module : ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ( ํ•ด๋‹น ํด๋ž˜์Šค, ํ•จ์ˆ˜๊ฐ€ ์–ด๋–ค ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•˜๋Š”์ง€์— ๋Œ€ํ•ด nest.js์— ์•Œ๋ ค์ฃผ๋Š” ์—ญํ• )

  • imports : ํ•ด๋‹น ๋ชจ๋“ˆ์—์„œ ํ•„์š”ํ•œ ๋ชจ๋“ˆ์˜ ์ง‘ํ•ฉ์„ ์ •์˜.
    ์—ฌ๊ธฐ์— ๋“ค์–ด๊ฐ€๋Š” ๋ชจ๋“ˆ์€ ํ”„๋กœ๋ฐ”์ด๋”(์„œ๋น„์Šค)๋ฅผ ๋…ธ์ถœํ•˜๋Š” ๋ชจ๋“ˆ.
    ํ”ํžˆ ์ž„ํฌํŠธ๋ฅผ ํ•˜๋Š” ๋ชจ๋“ˆ : HttpModule, TypeOrmModule ๋“ฑ.

  • controllers : ํ•ด๋‹น ๋ชจ๋“ˆ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ •์˜.

  • providers : ํ•ด๋‹น ๋ชจ๋“ˆ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์„œ๋น„์Šค๋ฅผ ์ •์˜.

  • exports : ํ•ด๋‹น ๋ชจ๋“ˆ์—์„œ ๋…ธ์ถœ์‹œํ‚ฌ ์„œ๋น„์Šค๋ฅผ ์ •์˜.
    ๋‹ค๋ฅธ ๋ชจ๋“ˆ์—์„œ ํ•ด๋‹น ๋ชจ๋“ˆ์˜ ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” exports๋กœ ๋…ธ์ถœ์„ ํ•ด์•ผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ.


โœ”๏ธ app.controller.ts

import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {} 
  //์ธ์ž๋กœ AppService ๊ฐ์ฒด๋ฅผ ๋„˜๊ธฐ๋ฉด this.appService๋ผ๋Š” ๋ฉค๋ฒ„ ๋ณ€์ˆ˜์— AppService ๊ฐ์ฒด๊ฐ€ ์ฃผ์ž…

  @Get()
  getHello(): string {
    return this.appService.getHello();
  }
}
  • controller๋Š” service๋ฅผ ๋ฐ˜๋“œ์‹œ ์˜์กดํ•ด์•ผ ํ•˜๋ฉฐ ์ด๋Š” ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•œ DI(Dependency Injection)์œผ๋กœ ํ•ด๊ฒฐํ•ด์•ผ ํ•จ.

โœ”๏ธ app.service.ts

import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
  getHello(): string {
    return 'Hello World!';
  }
}
  • @Injectable : Service๋ฅผ ๋‹ค๋ฅธ ๊ณณ์—์„œ ์˜์กด์„ฑ ์ฃผ์ž…์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ์—ญํ• 
  • ์„œ๋น„์Šค ๊ฐ์ฒด๋Š” ์‹ค์ œ๋กœ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ์˜์กดํ•˜๋ฉฐ ๋น„์ง€๋‹ˆ์Šค ๋กœ์ง ์‹คํ–‰์„ ๋‹ด๋‹น
  • DB ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ : ์„œ๋น„์Šค๋Š” ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋ฅผ ๋ฐ˜๋“œ์‹œ ์˜์กดํ•ด์•ผ ํ•˜๋ฉฐ ์ด๋Š” ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•œ DI๋กœ ํ•ด๊ฒฐํ•ด์•ผ ํ•จ.

โœ”๏ธ ์ƒ์„ฑ ๋ช…๋ น์–ด

// module ์ƒ์„ฑ ๋ช…๋ น์–ด
nest g mo board

// controller ์ƒ์„ฑ ๋ช…๋ น์–ด
nest g co board

// service ์ƒ์„ฑ ๋ช…๋ น์–ด
nest g s board

// ํŒŒ์ดํ”„ ์ƒ์„ฑ ๋ช…๋ น์–ด
nest g pi board-status-validation.dto
  • ์ฐธ๊ณ : reposiotory๋Š” ์ง์ ‘ ์ƒ์„ฑํ•ด์•ผํ•จ.


IoC์™€ DI

โœ”๏ธ IoC (Injection of Control)

์ œ์–ด ์—ญ์ „

IoC๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๊ฐ์ฒด์˜ ์ƒ๋ช…์ฃผ๊ธฐ ๊ด€๋ฆฌ ์ž์ฒด๋ฅผ ์™ธ๋ถ€(์—ฌ๊ธฐ์„œ๋Š” Nest.js IoC ์ปจํ…Œ์ด๋„ˆ)์— ์œ„์ž„์„ ํ•จ.
์ฆ‰, ๊ฐ์ฒด์˜ ๊ด€๋ฆฌ๋ฅผ ์ปจํ…Œ์ด๋„ˆ์— ๋งก๊ฒจ์„œ ์ œ์–ด๊ถŒ์ด ๋„˜์–ด๊ฐ”๊ธฐ ๋•Œ๋ฌธ์— IoC(์ œ์–ด ์—ญ์ „)๋ผ๊ณ  ํ•œ๋‹ค.

IoC๋Š” ๋ชจ๋“ˆ ๊ฐ„ ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถ”๊ธฐ ๋•Œ๋ฌธ์— ํ•˜๋‚˜์˜ ๋ชจ๋“ˆ์ด ๋ณ€๊ฒฝ๋˜์–ด๋„ ๋‹ค๋ฅธ ๋ชจ๋“ˆ๋“ค์—๋Š” ์˜ํ–ฅ์„ ์ตœ์†Œํ™”๋˜์–ด ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ง€์† ๊ฐ€๋Šฅํ•˜๊ณ  ํ™•์žฅ์„ฑ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

โœ”๏ธ DI (Dependency Injection)

DI๋Š” ์ด๋Ÿฐ IoC๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์˜ ํ•˜๋‚˜์ด๋ฉฐ Nest.js์—์„œ๋Š” ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•œ DI๋ฅผ ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ IoC ํ…Œํฌ๋‹‰์œผ๋กœ ์ƒ๊ฐํ•˜๊ณ  ์žˆ์Œ.



profile
์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ๋ฟ ๋‚ด๊ฐ€ ๋ชปํ•  ๊ฑด ์—†๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€