简体   繁体   English

NestJS:记录HttpService调用中的请求/响应吗?

[英]NestJS: Logging the request/response from HttpService calls?

I was wondering if its possible to log the requests, response and errors using the HttpService from the HttpModule . 我想知道是否有可能使用HttpModuleHttpService记录请求,响应和错误。

I used to use Interceptors from AXIOS, HttpService wraps axios but I can't seem to add any interceptors here, there doesn't seem to be a place in 我曾经使用过AXIOS的拦截器, HttpService封装了axios,但是我似乎无法在此处添加任何拦截器,在这里似乎没有位置

HttpModule.register(...)

Then I thought that NestJS comes with its own interceptors and wondered if its possible to use NestJS interceptors. 然后我以为NestJS带有自己的拦截器,想知道是否有可能使用NestJS拦截器。

I wouldn't want to apply the interceptor over a controller, service but apply it to the HttpService ? 我不想将拦截器应用于控制器,服务,而是将其应用于HttpService

Any ideas, a little lost how to do this in the nestjs way. 任何想法,都有点失去了如何以nestjs方式执行此操作。

Thanks in advance 提前致谢

The HttpService exposes its axios instance directly via get axiosRef() . HttpService通过get axiosRef()直接公开其axios实例。 With it, you can add an axios interceptor : 有了它,您可以添加axios interceptor

this.httpService.axiosRef.interceptors.request.use(config => console.log(config));

You can for example do that in the onModuleInit() of your AppModule . 例如,您可以在onModuleInit()AppModule

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM