gpt4 book ai didi

javascript - 在 Nest.js 应用程序的 `main.js` 内使用 ConfigService

转载 作者:行者123 更新时间:2023-11-28 16:56:29 26 4
gpt4 key购买 nike

我正在尝试弄清楚如何使用我的 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 内的 getter。我可以访问此级别的服务吗?

最佳答案

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

关于javascript - 在 Nest.js 应用程序的 `main.js` 内使用 ConfigService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59019131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com