Nest JS

: node.js & express ์œ„์—์„œ ์›€์ง์ด๋Š” ํ”„๋ ˆ์ž„์›Œํฌ => node.js์— ๋ฐฑ์—”๋“œ ๊ตฌ์„ฑํ• ์ˆ˜ ์žˆ๊ฒŒ ํ•จ

โ€“> node.js๋Š” ์ œ์•ฝ์ด ์—†์Œ(api๋ฅผ ๋งŒ๋“ค๋–„๋„ url์„ ์–ด๋””๋‹ค ๋‘˜์ง€, ์ปจํŠธ๋กค๋Ÿฌ, ํ…œํ”Œ๋ฆฟ ์œ„์น˜๋„ ๋งˆ์Œ๋Œ€๋กœ)

=> nest.js๋Š” ๊ตฌ์กฐ๊ฐ€ ์žˆ์–ด์„œ ์‰ฝ๊ฒŒ ๋ฐฑ์—”๋“œ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ

Nest JS๋กœ rest API ๋งŒ๋“ค๊ธฐ

  • ์„ค์ •
  1. insomnia ์„ค์น˜

    => insomnia rest ๊ฒ€์ƒ‰

  2. nestjs/cli ์„ค์น˜

    1) npm i -g @nestjs/cli

    2) nestJS๋ฅผ ์‚ฌ์šฉํ•  ํŒŒ์ผ๋กœ ์ด๋™

     - nest new  
    
    - ํ”„๋กœ์ ํŠธ ์ด๋ฆ„

    3) cd ํ”„๋กœ์ ํŠธ ์ด๋ฆ„ & code .

  3. github์— repo ๋งŒ๋“ค๊ธฐ

  4. vscode terminal๋กœ

    • git remote add origin ๊นƒํ—™ repo ์ฃผ์†Œ
  • vscode
    1. src ํŒŒ์ผ์— app.controller.specํŒŒ์ผ ์ง€์šฐ๊ธฐ -> ๋‚˜์ค‘์— ๋‹ค์‹œ ๋งŒ๋“ค๊ฑฐ
    2. npm run start:dev -> ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰
    3. localholst:3000์œผ๋กœ ์ ‘์†
  • main.ts

    ์ด๋ฆ„๋ณ€๊ฒฝ ๊ธˆ์ง€

    import { NestFactory } from '@nestjs/core';
    import { AppModule } from './app.module';
      
    async function bootstrap() {   
      const app = await NestFactory.create(AppModule); // await NestFactort.creat(AppModule)ํ˜ธ์ถœ 
      await app.listen(3000); //3000๋ฒˆ์˜ ํฌํŠธ ๋ฆฌ์Šค๋‹
    }
    bootstrap();
    

    -> ํ•˜๋‚˜์ธ ๋ชจ๋“ˆ(AppModule)์—์„œ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ƒ์„ค

    • AppModule: ๋ชจ๋“  ๊ฒƒ์˜ ๋ฃจํŠธ ๋ชจ๋“ˆ (๋ชจ๋“ˆ์€ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ผ๋ถ€๋ถ„)
  • app.module.ts

    ->main.ts์˜ AppModule

    import { Module } from '@nestjs/common';
    import { AppController } from './app.controller';
    import { AppService } from './app.service';
      
    @Module({    // ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ
      imports: [],
      controllers: [AppController],
      providers: [AppService],
    })
    export class AppModule {}
      
    
    • ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ: ํด๋ž˜์Šค์— ํ•จ์ˆ˜ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ (=ํด๋ž˜์Šค ์œ„์˜ ํ•จ์ˆ˜)
      • controllers: url์„ ๊ฐ€์ ธ์˜ค๊ณ  ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰
  • app.controller.ts

    -> app.module.ts ์˜ module์•ˆ์˜ controllers

    import { Controller, Get } from '@nestjs/common';
    import { AppService } from './app.service';
      
    @Controller()
    export class AppController {
      constructor(private readonly appService: AppService) {}
      
      @Get()//๋ฐ์ฝ”๋ ˆ์ดํ„ฐ
      getHello(): string { //stringํ˜• ๋ฐ˜ํ™˜
        return this.appService.getHello();
      }
    }
    
    • get ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ
  • app.service.ts

    -> app.controller.ts ์˜ Controller ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ ์•ˆ appservice

    import { Injectable } from '@nestjs/common';
      
    @Injectable()
    export class AppService {
      getHello(): string {
        return 'Hello World!';  //ํ™”๋ฉด์— getHelloํ•จ์ˆ˜์˜ ๋ฆฌํ„ด๊ฐ’ ํ‘œ์‹œ
      }
    }
    

NestJS ์‚ฌ์šฉ์˜ˆ์‹œ

-> main.ts๋กœ ๋ชจ๋“  ๊ฑธ ์‹œ์ž‘

  • app.controller.ts

    • Get ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ์— ์ƒˆ ํ•จ์ˆ˜
    import { Controller, Get } from '@nestjs/common';
    import { AppService } from './app.service';
      
    @Controller()
    export class AppController {
      constructor(private readonly appService: AppService) {}
      
      @Get()
      getHello(): string {
        return this.appService.getHello();
      }
      
      @Get("/hello")  // "/hello"๋ผ๋Š” url๋กœ์˜ ์š”์ฒญ์„ ๋ฐ›์•„
      sayHello(): string { //sayHello()๋ผ๋Š” ํ•จ์ˆ˜ ์‹คํ–‰
        return "Hello everyone";
      }
    }
    
    • ๋ฌด์กฐ๊ฑด ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ๋Š” ๊พธ๋ฉฐ์ฃผ๋Š” ํ•จ์ˆ˜๋‚˜ ํด๋ž˜์Šค๋ž‘ ๋ถ™์–ด์žˆ์–ด์•ผํ•จ!(๊ณต๋ฐฑ ์žˆ์œผ๋ฉด ์•ˆ๋จ)

    => localhost:3000/hello์—์„œ sayHelloํ•จ์ˆ˜ ์‹คํ–‰

Appservice๊ฐ€ ํ•„์š”ํ•œ ์ด์œ 

nestJs๋Š” ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ๋น„์ง€๋‹ˆ์Šค ๋กœ์ง๊ณผ ๊ตฌ๋ถ„

  • ์ปจํŠธ๋กค๋Ÿฌ: url๊ฐ€์ ธ์˜ค๊ณ  ํ•จ์ˆ˜ ์‹คํ–‰
  • ๋น„์ง€๋‹ˆ์Šค ๋กœ์ง: ์„œ๋น„์Šค
    • ์„œ๋น„์Šค: ์‹ค์ œ๋กœ function์„ ๋†“๋Š” ๊ณณ

app service

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

@Injectable()
export class AppService {
  getHello(): string {
    return 'Hello Nest!';
  }
  getHi():string{
    return "Hi Nest";
  }
}

app.controller.ts

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

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  getHello(): string {
    return this.appService.getHello();
  }

  @Get("/hello")
  sayHello(): string {
    return this.appService.getHi();    //appservice์˜ getHiํ•จ์ˆ˜ ์‚ฌ์šฉ
  }
}

app.module

-> ๋ฃจํŠธ๋ชจ๋“ˆ(NestJS๊ฐ€ ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋งŒ๋“ค๋–„ ์ด์šฉํ•˜๋Š” ๊ฒƒ)

=> ๋ชจ๋‘ ๋‹ค importํ•ด์•ผํ•จ