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

Tho*_*ggi 1 javascript middleware typescript nestjs

我想弄清楚如何使用我的 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();

Run Code Online (Sandbox Code Playgroud)

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

Jay*_*iel 5

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