🎇middleware 작성
@Injectable()
export class IpFilterMiddleware implements NestMiddleware {
use(req: Request, res: Response, next: NextFunction) {
let allowIps: string[] = [];
if (process.env.NODE_ENV == 'local') {
allowIps = ['127.0.0.1'];
}
if (!allowIps.includes(req.ip.toString())) {
throw new UnauthorizedException('invalid ip');
}
next();
}
}
🎇 middleware 주입
# app.module.ts
...
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(IpFilterMiddleware).forRoutes('/**');
}
}
반응형
'nodejs > nestjs' 카테고리의 다른 글
@nestjs-modules/ioredis로 redis queue 사용하기 (0) | 2022.11.01 |
---|---|
jwt role guard 적용기 (0) | 2022.02.07 |
eslint-plugin-import in nestjs (0) | 2022.01.26 |
custom repository (0) | 2021.08.24 |
dto에서 type 변환 적용하기 (0) | 2021.08.24 |