[Nestjs] 자동생성, 주요 데코레이션

Woong·2022년 11월 18일
0

Nestjs

목록 보기
2/28

CLI 로 자동생성

  • nest g controller <name>
  • nest g service <name>
  • nest g class <name>
     ┌───────────────┬─────────────┬──────────────────────────────────────────────┐
      │ name          │ alias       │ description                                  │
      │ application   │ application │ Generate a new application workspace         │
      │ class         │ cl          │ Generate a new class                         │
      │ configuration │ config      │ Generate a CLI configuration file            │
      │ controller    │ co          │ Generate a controller declaration            │
      │ decorator     │ d           │ Generate a custom decorator                  │
      │ filter        │ f           │ Generate a filter declaration                │
      │ gateway       │ ga          │ Generate a gateway declaration               │
      │ guard         │ gu          │ Generate a guard declaration                 │
      │ interceptor   │ itc         │ Generate an interceptor declaration          │
      │ interface     │ itf         │ Generate an interface                        │
      │ middleware    │ mi          │ Generate a middleware declaration            │
      │ module        │ mo          │ Generate a module declaration                │
      │ pipe          │ pi          │ Generate a pipe declaration                  │
      │ provider      │ pr          │ Generate a provider declaration              │
      │ resolver      │ r           │ Generate a GraphQL resolver declaration      │
      │ service       │ s           │ Generate a service declaration               │
      │ library       │ lib         │ Generate a new library within a monorepo     │
      │ sub-app       │ app         │ Generate a new application within a monorepo │
      │ resource      │ res         │ Generate a new CRUD resource                 │
      └───────────────┴─────────────┴──────────────────────────────────────────────┘

주요 데코레이터

  • @Controller : 컨트롤러 지정

    • @Controller 데코레이터의 인자는 prefix
      • ex) @Controller('app')이라고 했다면
        • http://localhost:3000/hello -> http://localhost:3000/app/hello 로 접근
    • 하위 도메인 처리
      • @Controllerhost 속성으로 하위 도메인 처리 설정
      • ex) @Controller({ host: 'api.example.com' }) // 하위 도메인 요청 처리 설정
  • @Param : 요청 파라미터 처리

  • @Header : 응답에 커스텀 헤더 추가
    @Header('Custom', 'Test Header')

  • @Redirect : 리다이렉트

    • ex) @Redirect('https://nestjs.com', 301)
@Get('docs')
@Redirect('https://docs.nestjs.com', 302)
getDocs(@Query('version') version) {
  if (version && version === '5') {
    return { url: 'https://docs.nestjs.com/v5/' };
  }
}
  • @Body : HTTP Body 처리

  • @Injectable : 주입될 수 있는 프로바이더로 지정

  • @Inject : 생성자가 아닌 필드 주입

    • 상속할 경우 @Inject 를 사용하는 것이 편리
    • ex)
export class BaseService {
  @Inject(ServiceA) private readonly serviceA: ServiceA;
  ...
}

Provider

  • 프로바이더 : 비즈니스 로직을 처리하는 역할
    • Service, Repository, Factory, Helper 등
    • 기본적으로 캡슐화되기 때문에, DI 로 주입해주어야한다.

reference

0개의 댓글