繁体   English   中英

在 Nest.js 应用程序的 main.js 中使用 ConfigService

[英]Use ConfigService within `main.js` of Nest.js application

我想弄清楚如何使用我的 ConfigService创建的,如此处所述 在我的应用程序的最顶层。

我的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内的吸气剂。 我可以访问此级别的服务吗?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM