简体   繁体   中英

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 .

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

HttpModule.register(...)

Then I thought that NestJS comes with its own interceptors and wondered if its possible to use NestJS interceptors.

I wouldn't want to apply the interceptor over a controller, service but apply it to the HttpService ?

Any ideas, a little lost how to do this in the nestjs way.

Thanks in advance

The HttpService exposes its axios instance directly via get axiosRef() . With it, you can add an axios interceptor :

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

You can for example do that in the onModuleInit() of your AppModule .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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