Nest folder structure

박성운·2022년 7월 25일
0

Nest 폴더 구조
1.provider
많은 기본 Nest 캘래스들은 프로바이더롤 취급 될 것이다. 프로바이더는 객체들이 서로 관계 되도록 도와주는 일을 한다. 그리고 객체들의 연결은 Nest runtime system으로 대표 된다.

프로바이더들은 module에서 providers라고 명시된 평범한 클라스이다.
CatsService를 만드어 보자. 이 service는 데이터 스토어와 가져오는 것을 위해 만들어 졌다.

이 CatService 는 평범한 캘래스인데, 하나의 property와 두개의 method를 가지고 있습니다. 그리고 Cat interface를 사용해야 한다. 이렇게 새겼다.

export interface Cat {
  name: string;
  age: number;
  breed: string;
}

이제 contoller를 보자. CatService 는 class constructor에 의해 주입된다.
Nest는 Dependency inejction이라는 패턴으로 디자인 되어 있다. CatService는 타입으로 관리 될 수 있다. Nest will resolve the catsService by creating and returning instance of CatService.

constructor(private catsService: CatsService) {}

@option()fmf 사용해 optional providers 를 사용 할 수 있다.

If we have defined a provider(CatsService), and have the controller of the service(CatsController), we have to register with Nest so that it can perform the injection. We do this by editing our module profile(app.module.ts), and within the file we add the service to the providers array of the @Module() decorator.

  1. Config
    config/ 폴더에는 상황에 따라 필요한 환경변수들이 들어있습니다.
    indix.ts: 다른 모듈들을 export하는 barrel
    configuration.ts: process.env 환경 변수들을 읽어 @nestjs/config에 등록합니다.
    config.service.ts:외부 모듈들에 각 config 값들을 노출시켜주는 인터페이스 역할을 수행합니다.
    config.module.ts: ConfigModule.forRoot를 호출하고 service를 외부 모듈들에 export합니다.

0개의 댓글