我创建了一个身份验证中间件来检查每个请求,中间件正在使用服务器(仅当在 req.connection 中找不到数据时)。我正在尝试将服务注入到我的中间件中,但我不断收到相同的错误“Nest 无法解析 AuthenticationMiddleware (?) 的依赖项。请验证 [0] 参数在当前上下文中是否可用。”
身份验证模块:
@Module({
imports: [ServerModule],
controllers: [AuthenticationMiddleware],
})
export class AuthenticationModule {
}
身份验证中间件:
@Injectable()
export class AuthenticationMiddleware implements NestMiddleware {
constructor(private readonly service : UserService) {}
resolve(): (req, res, next) => void {
return (req, res, next) => {
if (req.connection.user)
next();
this.service.getUsersPermissions()
}
}
服务器模块:
@Module({
components: [ServerService],
controllers: [ServerController],
exports: [ServerService]
})
export class ServerModule {}
应用模块:
@Module({
imports: [
CompanyModule,
ServerModule,
AuthenticationModule
]
})
export class ApplicationModule implements NestModule{
configure(consumer: MiddlewaresConsumer): void {
consumer.apply(AuthenticationMiddleware).forRoutes(
{ path: '/**', method: RequestMethod.ALL }
);
}
}