javascript - Is there a way to load a .json configuration file and automatically substitute environment variables?
问题描述
I'm a little new to the javascript ecosystem, so apologies if I'm asking the question wrong.
I'm working on a legacy project that loads configuration files from .json files. In those .json files there are variables that need to be substituted with environment variables.
For example config.json:
{
"database": {
"host": "%DBHOST%",
"port": "%DBPORT%"
}
}
In the legacy code, there are complicated startup scripts that use sed
to do variable substitution. I'm moving the project into docker and would much rather define DBHOST/DBPORT etc in the docker definitions and have it magically update the configuration. Right now, I just have docker running a script that does the sed
operation which works, but this feels clunky. Is there a way I can tell node to load the .json file and automatically substitute the %VAR%
blocks with an environment variable if there is a match?
解决方案
使用 NodeJS,您可以像这样加载环境变量:
process.env.VARIABLE_NAME
推荐阅读
- sql - SQL时间戳到INT,最新记录
- python - 如何在pyqtgraph中设置刻度
- ms-access - Visual Studio 将 VB 项目连接到 .accde 数据库
- tortoisegit - 如何使用 TortoiseGit 从远程 git 存储库中获取笔记?
- c# - 无效的密钥哈希。我通过facebook登录游戏时出错
- javascript - Vue.js @click 只注册了一半的时间
- python - 尝试将 tkinter 中的图像正确显示到类的方法中
- go - go rpc 内存泄漏还是不了解?
- ruby - 更新 Ruby Mac OS 10.6.8 Snow Leopard
- javascript - (Javascript) 需要帮助将对象转换为数组