I've just started studying NestJS, so, I couldn't even simply figure out what could went wrong trying to add new variables from Interceptor to DOM via hbs parameters.
My studying task was just to print logging information about server response time somewhere on the generated pages using Interceptor.
But when try to use other solutions to do it, I have a problem that field for logging on the page is empty. So, how to solve it?
My Interceptor:
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable, map } from 'rxjs';
export interface Response<T> {
data: T;
}
@Injectable()
export class LoggingInterceptor<T> implements NestInterceptor<T, Response<T>> {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const start = Date.now();
return next
.handle()
.pipe(
map((data) => {return {...data, time: Date.now() - start}}),
);
}
}
My template page partial to post into by time value:
<div>
<h3>Contacts</h3>
<p>Some links</p>
<p>Total time server: {{time}}</p>
</div>
Then I added this Logging Interceptor to one existing Controller like UseInterceptors(LoggingInterceptor<any>)
and that is all.
And Interceptor work results nothing and I have no idea why.
PS: I tryed to use simple Controller method like:
@UseInterceptors(LoggingInterceptor)
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {
this.appService = appService;
}
@Get('/test')
doSmth() {
return {smt: 'aaa', title: 'test'};
}
}
And it shows result. But as I use hbs templates I need to use Controller methods like:
@UseInterceptors(LoggingInterceptor)
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {
this.appService = appService;
}
@Get('/')
root(@Res() res: Response<any>) {
return res.render(
'home',
{ layout: 'layout/layout_main', message: 'Hello world!' },
);
}
}