【问题标题】:Use ConfigService within `main.js` of Nest.js application在 Nest.js 应用程序的 `main.js` 中使用 ConfigService
【发布时间】:2019-11-24 15:13:20
【问题描述】:

我想弄清楚如何使用我的 ConfigService created as described here。在我的应用程序的最顶层。

我的main.js 文件如下所示:

import { NestFactory } from '@nestjs/core';
import { TypeormStore } from 'connect-typeorm';
import * as session from 'express-session';
import { getRepository } from 'typeorm';
import { Session } from '../../domains/session/Session';
import { AppModule } from './AppModule';

async function bootstrap() {
    const app = await NestFactory.create(AppModule);

    app.use(session({
        resave: false,
        saveUninitialized: false,
        store: new TypeormStore({
            cleanupLimit: 2,
            ttl: 86400
        }).connect(getRepository(Session)),
        secret: process.env.COOKIE_SECRET as string
    }))

    await app.listen(3000);
}
bootstrap();

我想要将process.env.COOKIE_SECRET 移动到ConfigService 内的吸气剂。我可以访问此级别的服务吗?

【问题讨论】:

    标签: javascript typescript middleware nestjs


    【解决方案1】:

    要从应用程序上下文中拉出任何服务,在工厂创建应用程序const app = await NestFactory.create(AppModule)之后,您可以使用应用程序的get方法拉出服务以使用const config = app.get<ConfigService>(ConfigService),然后您可以使用配置您创建的方法。

    【讨论】:

      猜你喜欢
      • 2018-09-05
      • 1970-01-01
      • 2023-03-09
      • 2022-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      相关资源
      最近更新 更多