When i create Logger middleware with Nestjs I want logger response || data of server send for client but in Response from express but it not found option
import { NextFunction, Request, Response } from 'express';
@Injectable()
export class EcomWebLogerMiddleware implements NestMiddleware {
private logger = new Logger();
use(request: Request, response: Response, next: NextFunction): void {
const { method, url: path, query, params, body } = request;
this.logger.log(
` ${method} ${path} query: ${JSON.stringify(
query
)} params: ${JSON.stringify(params)} body: ${JSON.stringify(body)}`,
'FE Request'
);
response.on('close', () => {
const { statusCode } = response;
// i want get data in response:Response from express
this.logger.log(`${method} ${statusCode} ${path} `, 'FE Response');
});
next();
}
}
With ExceptionFilter you can use ArgumentsHost after you can use it to getResponse
@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
constructor(private readonly responseUtils: ResponseUtils) {}
catch(exception: HttpException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const res = ctx.getResponse<Response>();
const status = exception.getStatus();
const response: any = exception.getResponse();
// this is response i want get
... }
CodePudding user response:
It would be better to implement with an interceptor. It is very similar to koa's middleware.
CodePudding user response:
can you show your package.json, try install "@types/express" dependency