0

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.

r00k13
  • 215
  • 3
  • 15

2 Answers2

1

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

Micael Levi
  • 5,054
  • 2
  • 16
  • 27
0

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
    }),
  );
};