inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

[코드팩토리] [초급] NestJS REST API 백엔드 완전 정복 마스터 클래스 - NestJS Core

여러개의 파이프 동시에 사용하기

직접 생성해서 사용하는 pipe class에 대한 질문입니다.

86

흔들거북이

작성한 질문수 6

0

안녕하세요!

 

DefaultValuePipe와 이번 강의에 나온 인자를 받는 Custom Pipe Class는 반드시 인자를 받아야 하는 구조이기 때문에 무조건 직접 생성해서 사용해야한다. 라고 이해하였는데 맞을까요?

 

또한, 강의에서 MaxLengthPipe와 MinLengthPipe 각각 @Injectable() 데코레이터를 붙여서 작성하셨는데, 직접 생성해서 사용하는거라면 굳이 필요없지 않을까 생각하여서 빼고 테스트 해보니 문제없이 잘 동작하는 것 같습니다. 특별히 붙여야만 하는 이유가 있는걸까요?!

 

 

javascript typescript rest-api nestjs backend

답변 2

1

코드팩토리

안녕하세요!

1) 인자를 받아야한다면 말씀하신게 맞습니다. (Custom Pipe예제가 아니라도 모두 해당됩니다)

2) Injectable 데코레이터는 IoC 컨테이너가 의존성 주입으로 인지 할 수 있도록 해주는 기능입니다. 만약에 '직접' 인스턴스화를 하고 있다면 필요 없는 기능입니다. (모듈의 providers 파라미터에 인스턴스화 하지 않고 클래스를 직접 주입해주는 것과 같습니다)

감사합니다!

0

흔들거북이

언제나 빠른 답변 감사드립니다~! 잘 이해하였습니다.

0

인프런 AI 인턴

안녕하세요, 인프런 AI 인턴입니다.

흔들거북이님, Directly creating and using Pipe classes like DefaultValuePipe and Custom Pipe classes in NestJS generally requires instantiation (new) when they need constructor arguments. Your understanding is correct if the Pipe requires arguments to be configured for specific use cases. Direct instantiation allows you to pass these configurations directly.

As for using @Injectable() for Pipes like MaxLengthPipe and MinLengthPipe, while it’s true that you might not strictly need @Injectable() if you are directly instantiating them, the decorator serves a purpose:

  1. Singleton Management: When decorated with @Injectable(), the NestJS Dependency Injection container can manage them, meaning they’ll be instantiated and shared as singletons wherever used, unless manually instantiated.

  2. Consistency and Scalability: Applying @Injectable() ensures consistency across your codebase, making it easier if later you decide to let the DI system manage these instances instead.

  3. Future-proofing: If your application grows or the Pipe logic becomes complex or starts depending on other services, having it as an injectable service becomes beneficial.

For more detailed information on this topic, you might find it useful to explore the NestJS documentation on Pipes. This will provide you with a broad overview and specific use cases on how NestJS handles dependency injection.

저는 질문자님의 이해를 돕고자 지식공유자님의 기존 답변을 참고하여 유사한 답변 링크를 추천해드리고 있습니다.
현재 베타 기능으로 답변이 만족스럽지 않을 수 있는 점 양해 부탁드립니다. 🙏
추가적으로 궁금한 점이 있으시면, 이어서 질문해 주세요. 곧 지식공유자께서 답변해 주실 것입니다.

cascade 질문

0

39

1

@types/bcrypt 설치과정이 누락된것같습니다.

0

47

1

process.env port key 에러

0

46

1

추상화

0

49

1

[공유] DTO optional 필드가 undefined로 잡혀 TypeORM 조건이 깨지는 현상

0

94

2

where 키워드가 들어가는 메서드와 아닌 메서드

0

56

1

BearerTokenGuard에서 db를 조회해서 유저 정보를 불러오는 이유?

0

69

1

app.controller app.service 는 지워도되나요?

0

69

1

@JoinColumn을 쓰는 경우와 안쓰는 경우의 차이

0

78

1

포트 3000에서 listen하는 곳까지 넘어가지 않습니다.

0

66

1

PickType 사용 시 `as const`를 꼭 사용해야 하나요?

0

102

2

socket connect 오류

0

96

2

강의를 들으면서 궁금한 점

0

97

2

DELETE 요청의 반환값은 어떤 기준으로 결정하는 게 좋을까요?

0

60

2

커리큘럼 질문

0

98

2

put 요청은 언제

0

90

3

typeorm VS prisma

0

347

2

142 강의 > 4:00 > 포스트멘 활용 관련 질문 드립니다.

0

76

2

User 데코레이터 버그 수정 전달드립니다.

0

67

1

git 주소 부탁드립니다.

0

112

2

nest g resource 명령어 에러

0

99

2

로그인 엔드포인트 관련 질문

0

84

2

yarn으로 express 다운 후 node 2_server.js 실행 안되는 경우

0

146

3

"흔히 사용되는 메서드" 강의 관련 질문입니다~

0

95

2