【问题标题】:Nestjs env variable with ClientsModuleOptions带有 ClientsModuleOptions 的 Nestjs 环境变量
【发布时间】:2021-11-03 14:17:55
【问题描述】:

我有以下 IP 和端口,但我想将其与环境变量一起携带,以便可以从那里进行编辑

import { ClientsModuleOptions, Transport } from "@nestjs/microservices"

export const GatewayOptions: ClientsModuleOptions = [
    {
        name: 'MICRO-ADMIN',
        transport: Transport.TCP,
        options: {
            host: '127.20.20.2',
            port: 4000,
        },
    },
    {
        name: 'MICRO-DEV',
        transport: Transport.TCP,
        options: {
            host: '127.30.30.3',
            port: 5000,
        },
    },
];

我将此配置导入模块。

import { Module } from '@nestjs/common';
import { ClientsModule } from '@nestjs/microservices';
import { GatewayOptions } from 'src/utils/gateway/gateway';
import { AuthModule } from './../auth/auth.module';
import { CategoryModule } from './../category/category.module';
import { GameController } from './game.controller';
import { GameService } from './game.service';

@Module({
  imports: [
    AuthModule,
    CategoryModule,
    ClientsModule.register(GatewayOptions)
   ],
  controllers: [GameController],
  providers: [GameService],
  exports: [GameService],
})
export class GameModule {}

【问题讨论】:

    标签: node.js tcp ip nestjs nest


    【解决方案1】:

    你需要使用 register async 才能使用ConfigService

    关注文档:https://docs.nestjs.com/microservices/basics#client

    @Module({
      providers: [
        {
          provide: 'MATH_SERVICE',
          useFactory: (configService: ConfigService) => {
            const mathSvcOptions = configService.getMathSvcOptions();
            return ClientProxyFactory.create(mathSvcOptions);
          },
          inject: [ConfigService],
        }
      ]
      ...
    })
    

    以下是配置ConfigService的方法:https://docs.nestjs.com/techniques/configuration#configuration

    【讨论】:

      猜你喜欢
      • 2019-09-29
      • 2020-03-12
      • 2020-08-09
      • 2022-01-07
      • 2021-08-01
      • 2021-05-08
      • 1970-01-01
      • 2013-06-02
      • 2019-01-17
      相关资源
      最近更新 更多