본문 바로가기
개발/Nest

nest와 친해지기

by 안뇽! 2022. 6. 18.
반응형

백엔드 개발자가 없는 팀에서, 서버작업을 매번 다른팀에 요청해야하는 상황이다.

 

"그냥 내가 서버개발도 하자" 라는 생각에 회사 성장지원비로 인프런 nest 강의를 샀다. 

 

 

controller.ts

데코레이터

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

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

  @Get() //데코레이터, 함수나 클래스에 기능을 첨가하여 재사용성 극대화, 다음 줄에 바로 코드가 있어야 한다.(10번줄)
  getHello(): string {
    return this.appService.getHello();
  }
}

 

service.ts

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

@Injectable()
export class AppService {
  getHello(): string {
    //비즈니스 로직
    //리턴문은 express의 res.send()라고 생각하면 된다.
    //리턴값을 컨트롤러가 받고
 

    return 'Hello World!';
  }
}

 

module.ts

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

   //컨트롤러에서 모듈로 들어온다. 그 후 main.ts의 NestFactory로 전달

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

 

main.ts

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

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(8000);
}
bootstrap();
반응형

'개발 > Nest' 카테고리의 다른 글

Nest로 SSR하기 (+MVC)  (0) 2022.06.24
Nest 컨트롤러  (0) 2022.06.19