2

I am trying to figure out how I can us my ConfigService created as described here. Within the top-most level of my application.

My main.js file looks like this:

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();

What I'd like is to move process.env.COOKIE_SECRET to a getter inside ConfigService. Can I access services at this level?

1 Answer 1

8

To pull any service out from the application context after the factory has created the application (normally after const app = await NestFactory.create(AppModule)) you can then use the app's .get() method to pull out the service for use:

const config = app.get<ConfigService>(ConfigService);

From here, you can use the config methods you've created.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.