Nestjs Config access to config in bootstrap level

喜夏-厌秋 提交于 2020-12-14 02:27:09

问题


According to this documentation you import your config in AppModule.
I'm trying to access to config in bootstrap level in my main.ts file. Something like this:

const app = await NestFactory.create(AppModule);
  if (config.get('swagger.enabled'))
  {
    initSwagger(app);
  }
  await app.listen(8080);

The problem that I don't have access to config in this point, only other moudle will get access to config like this:

@Injectable()
export class SomeService {
    constructor(private readonly httpService: HttpService,
                  private readonly config: ConfigService) {}
}

My question: How to access to 'nestjs-config' in bootstrap level


回答1:


In your main.ts you can do const config = app.get(ConfigService) and have access to your ConfigService after you have created your server, but before you start listening on the port.



来源:https://stackoverflow.com/questions/59460475/nestjs-config-access-to-config-in-bootstrap-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!