Kevin Brustolin
Kevin Brustolin

Reputation: 73

Right way to get ConfigService when bootstrap microservice

I would like to know if i'm getting ConfigService the right way during bootstrap my microservice.

Is there a way to do it using NestFactory.createMicroservice()?

async function bootstrap() {
  const app = await NestFactory.create(CoreModule, {
    logger: new MyLogger(),
  });
  const configService: ConfigService = app.get(ConfigService);
  app.connectMicroservice({
    transport: Transport.TCP,
    options: {
      port: configService.PORT,
    },
  });
  await app.startAllMicroservicesAsync();
}

Upvotes: 6

Views: 5154

Answers (2)

Cristopher
Cristopher

Reputation: 33

Since NestJS v11.0.0, you can configure microservices using asynchronous options resolved from the dependency injection (DI) container. This enhancement allows for more dynamic and flexible microservice configurations. Here’s how you can implement this:

Example: Configuring a Microservice with Asynchronous Options

import { NestFactory } from '@nestjs/core';
import { PaymentsModule } from './payments.module';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { ConfigService } from '@nestjs/config';

async function bootstrap() {
  const app = await NestFactory.createMicroservice<AsyncOptions<MicroserviceOptions>>(PaymentsModule, {
    inject: [ConfigService],
    useFactory: (configService: ConfigService) => ({
      transport: Transport.TCP,
      options: {
        host: '0.0.0.0',
        port: configService.get<number>('PORT', 5001),
      },
    })
  });

  await app.listen();
}

bootstrap();

In this example:

  • Dependency Injection: The ConfigService is injected to access environment variables or configuration settings.
  • Asynchronous Factory: The useFactory function utilizes the injected ConfigService to dynamically set the microservice’s host and port.

This feature was introduced in Pull Request #12622 of the NestJS repository.

Upvotes: 0

Jay McDoniel
Jay McDoniel

Reputation: 70510

Yes there is a way to do it in the NestFactory, you're already doing it the right way! If you'd like another example, this is my bootstrap function:

async function bootstrap() {
  const app = await NestFactory.create(AppModule, {
    logger: new MyLogger()
  });
  const config = app.get<ConfigService>(ConfigService);
  const port = config.get('PORT');
  configure(app, config);
  await app.listen(port);
  scribe.info(
    `Listening at http://localhost:${port}/${config.get('GLOBAL_PREFIX')}`
  );
}

Where MyLogger is a custom implementation of the Nest default logger and configure(app, config) is a lot of application configuration done in a separate file to keep the bootstrap function lean and easy to maintain. Of course, the "listening at ..." also needs to be changed before heading to production, but this app is still in development for me.

The only thing I would suggest to you is changing

const configService: ConfigService = app.get(ConfigService);

to

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

using the generics for app.get() to give you the type information.

Upvotes: 9

Related Questions