从Nestjs中的自定义函数访问本地.ENV变量

发布于 2025-02-12 13:43:19 字数 896 浏览 0 评论 0原文

我有一个带有以下变量的.env.development文件

DB_NAME=db.sqlite
COOKIE_KEY=sdfjshdfjsf

,我创建了一个setup-app.tssetupapp函数,在哪里定义了cookie 值:

import { ValidationPipe } from '@nestjs/common';
const cookieSession = require('cookie-session');

export const setupApp = (app: any) => {
  app.use(
    cookieSession({
      keys: ['asdfasdf'],
    }),
  );

  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true,
    }),
  );
};

如何正确访问此.ENV变量(cookie_key = sdfjshdfjsfsetupapp函数?稍后,我想将我的应用上传到Heroku,并在此处创建此cookie_key变量。因此,它也应该以后与Heroku一起使用。

我在Nestjs找到了通过这种方法来进行此操作的方法,

keys: [this.configService.get('COOKIE_KEY')]

但是这显然需要structionor和进一步复杂的配置中进行依赖项注入。就我而言,我只有一个功能。

I have an .env.development file with the following variables

DB_NAME=db.sqlite
COOKIE_KEY=sdfjshdfjsf

For this I have created a setup-app.ts file with the setupApp function, where I defined a cookie keys value:

import { ValidationPipe } from '@nestjs/common';
const cookieSession = require('cookie-session');

export const setupApp = (app: any) => {
  app.use(
    cookieSession({
      keys: ['asdfasdf'],
    }),
  );

  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true,
    }),
  );
};

How to properly access this .env variable (COOKIE_KEY=sdfjshdfjsf) value within my setupApp function? Later I want to upload my app to Heroku and also create this COOKIE_KEY variable there. So it should also work later with Heroku.

I found a way in NestJS to do this via

keys: [this.configService.get('COOKIE_KEY')]

but this apparently requires a class with a dependency injection in the constructor and further complicated configurations. In my case I have only a function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨曦÷微暖 2025-02-19 13:43:19

只是nodejs。如果您想加载env。将process.env对象的文件归档,您可以使用一些lib,例如 dotenv @nestjs/config使用lib btw

it's just nodejs. If you want to load your env. file into process.env object, you can use some lib like dotenv. @nestjs/config uses that lib btw

触ぅ动初心 2025-02-19 13:43:19

创建configservice的新实例,然后使用configservice.get

import { ValidationPipe } from '@nestjs/common';
// import { CookieSession } from 'cookie-session';
const cookieSession = require('cookie-session');
import { ConfigService } from '@nestjs/config';

const configService = new ConfigService();

export const setupApp = (app: any) => {
  app.use(
    cookieSession({
      // keys: ['asdfasdf'],
      keys: [configService.get<string>('COOKIE_KEY')],
    }),
  );
  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true, //doesn't allow extra property in body like admin: 
true
    }),
  );
};

Create new instance of ConfigService then use configService.get

import { ValidationPipe } from '@nestjs/common';
// import { CookieSession } from 'cookie-session';
const cookieSession = require('cookie-session');
import { ConfigService } from '@nestjs/config';

const configService = new ConfigService();

export const setupApp = (app: any) => {
  app.use(
    cookieSession({
      // keys: ['asdfasdf'],
      keys: [configService.get<string>('COOKIE_KEY')],
    }),
  );
  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true, //doesn't allow extra property in body like admin: 
true
    }),
  );
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文