【发布时间】:2019-09-17 04:47:08
【问题描述】:
由于 NestJS 允许注入,我想确保我编写最有效的代码。
我正在使用全局拦截器来包装我的应用响应,并使用全局过滤器来处理异常。
//main.ts:
app.useGlobalInterceptors(new ResponseWrapperInterceptor(app.get(LogService)));
app.useGlobalFilters(new ExceptionsFilter(app.get(LogService)));
//filter/interceptor.ts:
constructor(@Inject('LogService') private readonly logger: LogService) {}
在我的 main.ts 中,什么更有效?这两种选择有什么影响?有没有更好的办法?
//Option 1:
app.useGlobalInterceptors(new ResponseWrapperInterceptor(app.get(LogService)));
app.useGlobalFilters(new ExceptionsFilter(app.get(LogService)));
或
//Option 2:
app.useGlobalInterceptors(new ResponseWrapperInterceptor(new LogService()));
app.useGlobalFilters(new ExceptionsFilter(new LogService()));
【问题讨论】:
标签: nestjs