0

我可以从我的 .env 文件中成功加载任何 env 变量,即已process.env.APP_PORT解决。但我也想在调用process.env之前加载一个变量。app.listen

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { kafkaMicroserviceConfig} from "./msKafkaConfig";

async function bootstrap() {

  app.enableCors();

  app.connectMicroservice(kafkaMicroserviceConfig);

  await app.startAllMicroservices();
  
  await app.listen(process.env.APP_PORT || 3000); // 3000

}
bootstrap().catch(console.error);;

在这种情况下,变量位于我作为kafkaMicroserviceConfig. 在那里,我想用process.env.KAFKA_BROKER_LIST.

但是,该 env 变量不会按时解析(与 .env 文件中的其他变量不同)。它抱怨“未定义”错误。

我可以在 NestJS/nodejs 应用程序中做什么来更早地读取该变量?

import {KafkaOptions, Transport} from "@nestjs/microservices";
import { ConfigService } from '@nestjs/config';

export const kafkaMicroserviceConfig: KafkaOptions = {
    transport: Transport.KAFKA,

    options: {
        client: {
            //brokers: ["localhost:9092"],
            brokers: [process.env.KAFKA_BROKER_LIST],
            retry: {
                initialRetryTime: 100,
                factor: 0.2,
                multiplier: 3,
                retries: 100
              }            
        },
        consumer: {
            groupId: 'trading-signal-listener',
            allowAutoTopicCreation: true,            
        },
        subscribe: {
            fromBeginning: true,
        }
    }
};

这是选项文件。

import {KafkaOptions, Transport} from "@nestjs/microservices";
import { ConfigService } from '@nestjs/config';

export const kafkaMicroserviceConfig: KafkaOptions = {
    transport: Transport.KAFKA,

    options: {
        client: {
            brokers: ["localhost:9092"],
            //brokers: [process.env.KAFKA_BROKER_LIST],
            retry: {
                initialRetryTime: 100,
                factor: 0.2,
                multiplier: 3,
                retries: 100
              }            
        },
        consumer: {
            groupId: 'trading-signal-listener',
            allowAutoTopicCreation: true,            
        },
        subscribe: {
            fromBeginning: true,
        }
    }
};
4

0 回答 0