Nestjs::nomad::0.2~

YP J·2023년 2월 7일
0

Nestjs

목록 보기
1/6

니꼬형 강의영상

https://www.youtube.com/watch?v=3JminDpCJNE
이건 노마드 다 듣고 따라 하자

  • 따라하면서 이해 안되는건 공식홈페이지 참고하면서 하기
  • 시작부터

nest new 에러


  • npm run start:dev 랑
  • nest start --watch 랑 같네
  • package.json 에 있는거.

  • AppModule 은 클래스다 app.module.ts 파일안에
@Module({
  imports: [],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
  • 데코레이터 (@) 는 클래스에 함수 기능을 추가할수 있다.

  • 다 지우고 처음부터 해보자

1.1 Controller

  • NestJS 어플리케이션은 여기 이 main.ts 에서 시작한다.
  • const app = await NestFactory.create(AppModule);
    • 하나의 모듈에서 어플리케이션을 생성한다.
    • AppModule은 모든것의 루트 모듈 같은것이다.

@Module({
  imports: [],
  controllers: [AppContoller],
  providers: [AppService],
})
  • Controller가 하는 일은 기본적으로 url 을 가져오고 함수를 실행하는것이다.
    • express 의 라우터 같은 존재: url을 가져오고 함수를 실행한다.
  • url을 가져와서 @데코레이터아래 에 있는 함수를 실행하는것.
  @Post("/hello")
  sayHello():string {
    return "hellp asldklasdj"
  }
  • 이렇게 문자열을 리턴 해도 홈페이지에 문자열 잘 뜨는데 왜 아래처럼
  @Get()
  getHello(): string {
    return this.appService.getHello();
  }
  • this.appService.getHello() 를 참조해서 리턴하고 있을까?

  • 그건 구조와 아키텍쳐 때문이다.

  • controller는 그냥 url을 가져오는 역할 + 함수 실행이다.

  • 실질적인 로직은 Service에서 한다.

  • 그래서 app.Service.ts 에

@Injectable()
export class AppService {
  getHello(): string {
    return '박준영!';
  }
  getHi():string {
    return "hi Nest"
  }
}
  • 문자열 반환하는 실질적인 로직이 있고
  • url가져오고 함수 실행하는 app.controller.ts 파일엔
  @Get()
  getHello(): string {
    return this.appService.getHello();
  }

  @Get("/hello")
  sayHello():string {
    return this.appService.getHi();
  }
  • 이렇게 app.Service.ts 파일에 있는 함수를 실행 시켜 놓는다.

  • app.module.ts 에 모든 필요한 모듈을 넣을거다 왜냐면 AppModule은
  • main.ts에서
async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
  • NestJS 가 우리의 어플리케이션을 만들기위해 AppModule을 이용하기 때문이다.

  • 이제 진짜 다 지우고 시작해보자
profile
be pro

0개의 댓글