【问题标题】:Setting up the environment vars in node before the config module reads the config from default.json在配置模块从 default.json 读取配置之前在节点中设置环境变量
【发布时间】:2021-04-13 13:27:41
【问题描述】:

对 JS 和节点比较陌生,

要求: 需要在加载配置模块之前从安全存储中读取后设置环境变量。

详情: 我们有一个问题,在配置模块启动并使用 default.js 分配值之前,我们需要从秘密存储中读取秘密并使其可用于节点环境。

我尝试拆分快速应用设置和初始化,并尝试在这些步骤之间读取秘密,但无济于事。

app.js -> Setup the app

index.js -> 
// Setup the environments with the secrets
import config from 'config'
import {app_init} from './app' // Setting up the express app

// Now setting up the env variables
process.env.DB_HOST = 'MyDbHost'
process.env.DB_PASS = 'DBPass' // This will be read from the secret store

log.info(`DB_HOST used in ENV: ${process.env.DB_HOST}`) // Prints the right value
log.info(`DB_HOST used in config: ${config.database.host}`) // Prints undefined

无论如何,有没有办法在配置模块读取它们之前将秘密注入环境中?

如果您需要更多信息,请告诉我。

【问题讨论】:

    标签: node.js node-config


    【解决方案1】:

    查看配置文档,尤其是here,可以使用延迟配置和使用完整的 nodejs 功能来创建配置。


    就我而言,我计划从商店中读取机密并将它们作为环境变量发布。如果我看到这样做的挑战,我会发布。

    【讨论】:

      猜你喜欢
      • 2019-09-28
      • 2019-08-26
      • 1970-01-01
      • 2015-11-10
      • 2010-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-06
      相关资源
      最近更新 更多