控制器上可以启用拦截器,方法里面也可以;还可以全局启用,待会把控制器的去掉
This commit is contained in:
parent
9d695d3f52
commit
de3558a97d
|
@ -5,6 +5,8 @@ import { PostsModule } from './modules/posts/posts.module';
|
||||||
import { DemoMiddleware } from './core/middleware/demo.middleware'
|
import { DemoMiddleware } from './core/middleware/demo.middleware'
|
||||||
import { APP_GUARD } from '@nestjs/core';
|
import { APP_GUARD } from '@nestjs/core';
|
||||||
import { DemoRolesGuard } from './core/guards/demo-roles.guard'
|
import { DemoRolesGuard } from './core/guards/demo-roles.guard'
|
||||||
|
import { APP_INTERCEPTOR } from '@nestjs/core';
|
||||||
|
import { LoggingInterceptor } from './core/interceptors/logging.interceptor'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -16,6 +18,10 @@ import { DemoRolesGuard } from './core/guards/demo-roles.guard'
|
||||||
{
|
{
|
||||||
provide: APP_GUARD,
|
provide: APP_GUARD,
|
||||||
useClass: DemoRolesGuard,
|
useClass: DemoRolesGuard,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provide: APP_INTERCEPTOR,
|
||||||
|
useClass: LoggingInterceptor,
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
|
|
|
@ -4,6 +4,8 @@ import { Observable } from 'rxjs';
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class LoggingInterceptor implements NestInterceptor {
|
export class LoggingInterceptor implements NestInterceptor {
|
||||||
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
|
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
|
||||||
|
console.log('I am a interceptor!')
|
||||||
|
|
||||||
return next.handle();
|
return next.handle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,15 @@
|
||||||
import { Controller, Get, Post, Req, Query, Headers, Param, Body, HttpException, HttpStatus, ForbiddenException, UseFilters, UsePipes, ValidationPipe, ParseIntPipe, UseGuards, SetMetadata } from '@nestjs/common';
|
import { Controller, Get, Post, Req, Query, Headers, Param, Body, HttpException, HttpStatus, ForbiddenException, UseFilters, UsePipes, ValidationPipe, ParseIntPipe, UseGuards, SetMetadata, UseInterceptors } from '@nestjs/common';
|
||||||
import { CreatePostDto } from './post.dto';
|
import { CreatePostDto } from './post.dto';
|
||||||
import { DemoService } from './providers/demo/demo.service'
|
import { DemoService } from './providers/demo/demo.service'
|
||||||
import { DemoFilter } from '../../core/filters/demo.filter'
|
import { DemoFilter } from '../../core/filters/demo.filter'
|
||||||
import { DemoAuthGuard } from '../../core/guards/demo-auth.guard'
|
import { DemoAuthGuard } from '../../core/guards/demo-auth.guard'
|
||||||
import { Roles } from '../../core/decorators/roles.decorator'
|
import { Roles } from '../../core/decorators/roles.decorator'
|
||||||
|
import { LoggingInterceptor } from '../../core/interceptors/logging.interceptor'
|
||||||
|
|
||||||
@Controller('posts')
|
@Controller('posts')
|
||||||
// @UseGuards(DemoAuthGuard)
|
// @UseGuards(DemoAuthGuard)
|
||||||
// @UseFilters(DemoFilter)
|
// @UseFilters(DemoFilter)
|
||||||
|
@ UseInterceptors(LoggingInterceptor)
|
||||||
export class PostsController {
|
export class PostsController {
|
||||||
|
|
||||||
constructor(private readonly demoService: DemoService) {}
|
constructor(private readonly demoService: DemoService) {}
|
||||||
|
|
Loading…
Reference in New Issue