Skip to content

Commit 894e698

Browse files
Merge branch 'raeisimv-docs-dynamic-config-microserivces'
2 parents 7177ebb + 23d4c3d commit 894e698

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

content/microservices/basics.md

+29
Original file line numberDiff line numberDiff line change
@@ -535,3 +535,32 @@ You can also pass an array of CAs if your setup involves multiple trusted author
535535
Once everything is set up, you can inject the `ClientProxy` as usual using the `@Inject()` decorator to use the client in your services. This ensures encrypted communication across your NestJS microservices, with Node's `TLS` module handling the encryption details.
536536

537537
For more information, refer to Node’s [TLS documentation](https://nodejs.org/api/tls.html).
538+
539+
#### Dynamic configuration
540+
541+
When a microservice needs to be configured using the `ConfigService` (from the `@nestjs/config` package), but the injection context is only available after the microservice instance is created, `AsyncMicroserviceOptions` offers a solution. This approach allows for dynamic configuration, ensuring smooth integration with the `ConfigService`.
542+
543+
```typescript
544+
import { ConfigService } from '@nestjs/config';
545+
import { AsyncMicroserviceOptions, Transport } from '@nestjs/microservices';
546+
import { AppModule } from './app.module';
547+
548+
async function bootstrap() {
549+
const app = await NestFactory.createMicroservice<AsyncMicroserviceOptions>(
550+
AppModule,
551+
{
552+
useFactory: (configService: ConfigService) => ({
553+
transport: Transport.TCP,
554+
options: {
555+
host: configService.get<string>('HOST'),
556+
port: configService.get<number>('PORT'),
557+
},
558+
}),
559+
inject: [ConfigService],
560+
},
561+
);
562+
563+
await app.listen();
564+
}
565+
bootstrap();
566+
```

0 commit comments

Comments
 (0)