Interceptor

tony·2023년 4월 26일
0

NestJS

목록 보기
2/3

무엇을 공부하였는가 🤔


Interceptor

  • 인터셉터란?
  • 인터셉터와 미들웨어의 차이점? :: 인터셉터의 목적? 존재이유?
    • chatGPT에게 물어봐!

      Q.
      i dont understand the purpose of interceptors in nestjs. we have middleware,pipeline and exception filter. why do we need or use interceptors??
      A.
      The key difference between middleware and interceptor in this example is that middleware operates at the request/response level, whereas interceptors operate at the method level. In other words, middleware can modify the request and response objects directly, whereas interceptors can only modify the input and output of the method they are intercepting.

      Q.
      why just use middleware then, not pre controller interceptors?
      A.
      Middleware and pre-controller interceptors serve similar purposes, but there are some differences between them that make each of them suitable for different use cases.
      Middleware in NestJS is used to execute logic before or after the controller method is called. Middleware is often used for cross-cutting concerns that affect multiple routes or controllers, such as authentication, logging, or error handling. Middleware functions have access to the request and response objects, and can modify them or terminate the request by sending a response.
      Pre-controller interceptors, on the other hand, are specifically designed to execute logic before the controller method is called. Interceptors can modify the incoming request object or the outgoing response object, but they cannot terminate the request by sending a response. Instead, interceptors can modify the response that is returned by the controller method, or they can throw an exception to trigger an error response.
      One advantage of pre-controller interceptors over middleware is that they are more focused and specialized. Interceptors are typically used for specific cross-cutting concerns, such as authentication, input validation, or caching, and they can be applied to individual controller methods rather than all routes in the application.
      Another advantage of interceptors is that they can be more powerful and flexible than middleware. Because interceptors have access to the execution context, they can inspect and modify the arguments passed to the controller method, and they can return observables or promises to control the flow of execution.
      In summary, middleware and pre-controller interceptors are both useful tools for implementing cross-cutting concerns in NestJS. Middleware is more general-purpose and can be used for a wide variety of use cases, while pre-controller interceptors are more specialized and focused on executing logic before the controller method is called. The choice between middleware and interceptors depends on the specific requirements of your application and the nature of the cross-cutting concern you are trying to implement.

솔직히 강사님께서 본인은 interceptor를 서비스의 return 값 데이터를 원하는 형식에 맞게 가공할 때 많이 사용한다는데, 모르겠다…

  1. 서비스 return 값을 가공할 것이라면 가공서비스를 하나 만들어서 의존하면 되는 것 아닌지?
    1. 의존성이 높아지기 때문에 그런 건가….
  2. 서비스 이후 인터셉터의 목적이 그러하다면, pre controller 인터셉터의 목적은 무엇인가??

어떻게 쓰는가 ☝️


왜 쓰는가 ❓


레퍼런스 🔍


profile
내 코드로 세상이 더 나은 방향으로 나아갈 수 있기를

0개의 댓글