Reputation: 79
Is it possible to access the Response Object from a service? You can access the Request from anywhere like this:
import { Inject, Injectable, Scope } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
@Injectable({ scope: Scope.REQUEST })
export class ResponseService {
constructor(@Inject(REQUEST) private readonly request: Request) {}
}
But is there a similar way for the Response or can I just create a new Response object?
Cheers
Upvotes: 2
Views: 492
Reputation: 5243
The reason that you have access to the request everywhere, is because it's already created. About response, when you call a service method from a controller, the response is not finalized and will be created based on the return
value of the controller (which can be a service call or anything). Maybe if you can elaborate with more details we can provide more proper solution.
But you can access the response in filters if you have an exception. See here for more info: https://docs.nestjs.com/exception-filters
Upvotes: 2