Nest, Node의 프레임웍

00_8_3·2021년 1월 1일
0
post-thumbnail

Nest란

A progressive Node.js framework for building efficient, reliable and scalable server-side applications.

Node의 위 Express의 위인 프레임웍이다!

➕ 기본적으로 TypeScript로 작성된다!

Why Nest js??

Node js의 장점이자 단점
어떠한 룰을 안따라도 된다는 것이다.

하지만!

RubyRuby on Rails
PythonDjango
JavaSpring

같은 구조, 순서, 룰을 따르기만 한다면 큰 규모의 프로젝트를 만들수 있는 장점이 있다.
즉 위와 같은 프레임웍들의 장점을 가져온 것이 Nest js이다

Required

먼저 NestJS를 사용하기 전에
NodeJSExpress개발 경험이 필요하다!

Nest setting

$ npm i -g @nestjs/cli
$ nest new nest-project
$ cd nest-project

Nest Base Structure

VSCode에서 열어보면 디렉토리는 위 사진과 같은 구조 일것이다.

src폴더의

  • app.controller.ts
  • app.module.ts
  • app.service.ts
  • main.ts
    로 구성 되어있다.

1 main.ts

가장 기본이 되는 파일로 서버포트를 열어 주는 역할을 한다.
❗ 주의할점 파일명은 무조건 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();

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 {}

모든 것의 루트 모듈 같은 것! 오직 하나의 모듈만이 있을 수 있다!

3 app.controller.ts

import { Controller, Get } from '@nestjs/common';
import { get } from 'http';
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 'hello everyone';
  }
}

controller는 url을 가져오고 실행 하는것 이다.
라우터 기능을 한다.(Express 프레임웍의 네이밍과 헷갈릴 수 있다.)
❗ 주의할점 @Get("")와 아래의 코드가 떨어지면 안된다!

localhost:3000/hello로 접근 가능

3.1 Express와의 비교

@Get("/hello")는 express의 다음과 같은 코드와 동일하게 작동한다!

route("/hello", (req, res) => {
	res.send("hello everyone")
})

4 app.service.ts

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

@Injectable()
export class AppService {
  getHello(): string {
    return 'Hello World!';
  }
}

app.controller.ts의 getHello 함수를 정의한다.

service?? contorllers?

NestJS는 콘트롤러를 비지니스 로직과 구분 짓고 싶어한다!

  • 컨트롤러 : url 가져오는 역할
  • 서비스 : 나머지 비지니스 로직, 일반적으로 실제 function을 갖는 부분

다음과 같이 변경 할 수 있다!

app.controllers.ts

import { Controller, Get } from '@nestjs/common';
import { get } from 'http';
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();
  }
}

app.service.ts

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

@Injectable()
export class AppService {

  getHello(): string {
    return 'Hello World!';
  }
  getHi(): string {
    return 'Hi nest!';
  }
}

3개의 댓글

comment-user-thumbnail
2021년 1월 3일

Syntax Highlighting이 있으면 좋을 것 같습니다 :)

1개의 답글