在我以前的项目中(在使用NestJS之前),我构建了一个名为“路由收集器”的实用工具。基本上,它只是递归地收集和附加到Express应用程序的路由。例如,我可以使用ts装饰器和元数据反射API来改变它的行为:
@Controller("/test")
class TestController {
@Test
@Get("/")
private testRoute() {
// code
}
}如果@Test NODE_ENV被设置为production,则装饰器将使路由收集器“忽略”。我希望在NestJS中获得这种行为,但不能给出代码。
有人实现过这样的东西吗?如果是这样的话,我很想得到一些提示。
发布于 2020-06-22 13:04:50
你可以成为一名全球卫士。(https://docs.nestjs.com/guards)
@Module({
providers: [
{
provide: APP_GUARD,
useClass: TestGuard,
},
],
})
export class AppModule {}测试警卫:
export class TestGuard implements CanActivate {
constructor(private readonly reflector: Reflector) {}
canActivate(context: ExecutionContext): boolean {
const testOnly = this.reflector.get<boolean>('testonly', context.getHandler());
// return false if node_env is prod and the test decorator is set
}
}装修师会是这样的:
export const Test = () => SetMetadata('testonly', true);https://stackoverflow.com/questions/62513764
复制相似问题