我关注了NestJS's docs用于设置拦截器,但我面临以下问题:
Error: Cannot find module 'src/middleware/request.interceptor'
at Function.Module._resolveFilename (module.js:548:15)
at Function.Module._load (module.js:475:25)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/mypath/dist/product/controller/product.controller.js:15:31)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
src/middleware/request.interceptor.ts
@Injectable()
export class LoggingInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
console.log('Before...');
const now = Date.now();
return next
.handle()
.pipe(
tap(() => console.log(`After... ${Date.now() - now}ms`)),
);
}
}
src/product/controller/product.controller.ts
@Controller('product')
@UseInterceptors(LoggingInterceptor)
export class ProductController {
constructor(private configService: ConfigService) {}
@Get()
findAll(): String {
return "hello";
}
}
src/product/product.module
@Module({
imports: [ConfigModule],
controllers: [ProductController]
})
export class ProductModule {}
@Module({
imports: [ProductModule],
controllers: [AppController],
providers: [AppService, Logger],
})
export class AppModule {}
我严格遵循文档,我已经尝试重建和删除 dist
文件夹。我错过了什么吗?
最佳答案
刚刚找到原因。
问题是该死的 VsCode 自动导入路径。
在product.controller
中,自动生成的导入是
import { LoggingInterceptor } from "src/middleware/request.interceptor";
但应该是 import { LoggingInterceptor } from "../../middleware/request.interceptor";
关于javascript - 找不到模块拦截器 NestJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62545485/