question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use the config in main.ts?

See original GitHub issue

I want to set app port using nestjs-config, how to use it in main.ts ?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10

github_iconTop GitHub Comments

21reactions
manjufycommented, Jun 1, 2021

Specially for creating microservices, you would want to be able to configure host and port, this is how I did it

// config/config.ts

export const config = () => ({
  app: {
    host: process.env.HOST,
    port: parseInt(process.env.PORT),
  },
  database: {},
});

//main.ts

import { NestFactory } from '@nestjs/core';
import { Transport, MicroserviceOptions } from '@nestjs/microservices';
import { AppModule } from './app.module';
import { config } from './config/config';

async function bootstrap() {
  const appConfig = config().app;
  const host = appConfig.host;
  const port = appConfig.port;
  const microserviceOptions: MicroserviceOptions = {
    transport: Transport.TCP,
    options: {
      host,
      port,
    },
  };
  const app = await NestFactory.createMicroservice<MicroserviceOptions>(
    AppModule,
    microserviceOptions,
  );

  app.listen(() =>
    console.log(`Account service listning on port ${host}:${port}`),
  );
}

bootstrap();

//app.modules.ts

import { ConfigModule } from '@nestjs/config';
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AccountModule } from './account/account.module';
import { config } from './config/config';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      load: [config],
    }),
    AccountModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
21reactions
ahmadarifcommented, Dec 25, 2018

[solved]

const app = await NestFactory.create(AppModule);
await app.listen(app.get('ConfigService').get('app.port'));
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use config module on main.ts file - nestjs
I have set a config module, according to https://docs.nestjs.com/techniques/configuration then how can I use it on main.ts??
Read more >
Configuration | NestJS - A progressive Node.js framework
Hint The ConfigService is imported from the @nestjs/config package. And use it in our class: // get an environment variable const dbUser =...
Read more >
NestJS Config Module: Using environment variables - Tom Ray
Learn how to use environment variables (and other configuration values) in your NestJS projects with the Config Module.
Read more >
Config File Setup In NestJs - Abiral Sthapit - Medium
“Config File Setup In NestJs” is published by Abiral Sthapit. ... app-config.controller.ts ... Importing Config in the Main src/main.ts
Read more >
Loading Configuration Before Your Angular App Starts
Let this be DEV, STAGE and PROD for this case. What we NOT want to do is using the environment.stage.ts , environment.prod.ts ,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found