首页 > 解决方案 > 除了process.env之外,有没有办法在整个项目中访问节点js中的变量(aws-ssm)?

问题描述

我正在尝试从 aws 参数存储访问我的环境变量,但我不确定如何在所有文件中访问它们而不使其成为全局或将它们存储在 process.env 中。有没有更安全的方法。我想使用导出这些变量,但由于运行时的导出过程和我的 aws 参数是在那之后。谢谢。

注意:- 我没有使用无服务器环境,我可以通过变量名直接访问它们。

标签: node.jsamazon-web-servicesenvironment-variablesdotenvaws-parameter-store

解决方案


环境变量是在单个应用程序中进行通信的一种非常糟糕的方式。它们通常用作在多个应用程序之间进行通信的一种解决方案。

在应用程序中存储和获取配置参数的常规方法是使用模块。是的,模块通常包含代码,但没有什么能阻止您创建仅存储数据的模块。事实上,这是一种相当标准的存储设置机制。

JSON文件

对于配置文件等固定配置值,标准是只使用 json 文件。例如:

配置.json:

{
    "port": 3000
}

然后,您可以在任何需要的地方要求 json 文件:

主.js:

config = require('./config'); // node will automatically search for
                              // config.js or config.json

app.listen(config.port);

some_module.js:

config = require('../../config');

console.log('we connected via port', config.port);

JS文件

有些人觉得 JSON 限制太多,因为您不能在 JSON 文件中添加注释。大多数人通常会从常规的 js 模块中导出对象:

库/config.js:

let config = {
    port: 3000,              // port to listen to, LOOK! COMMENTS!!
    messagePrefix: 'Hello ', // also, don't need to quote property names
                             // also, can use single quotes for strings
                             // also, dangling quotes are supported
}

module.exports = config

主.js:

const config = require('./lib/config');

app.listen(config.port);

some_module.js:

const config = require('./lib/config');

console.log(config.messagePrefix + 'World'); //Hello World

要了解其require工作原理的一件事是,它将缓存module.exports模块生成的值。这使得节点模块表现得像单例。在上面的例子中,config变量main.jssome_module.js指向同一个对象!我将再次重复这一点,因为它很重要:在上面的示例中,node.js只有一个配置对象。这意味着您可以使用像config.js文件这样的纯数据模块在代码中的模块之间进行通信。例如,您可以这样做:

主.js:

const config = require('./lib/config');

config.messagePrefix = 'Goodbye ';
app.listen(config.port);

some_module.js:

const config = require('./lib/config');

console.log(config.messagePrefix + 'World'); //Goodbye World

从其他地方读取配置的 JS 文件

因为我们使用普通模块与我们的其余代码通信配置数据,所以我们可以运行我们想要生成配置的任何算法:

配置.js:

let config = {};

// Try to load default config:
try {
    let rawdata = fs.readFileSync('./default-config.json');
    config = JSON.parse(rawdata);
}
catch (err) {
    // cannot load default config, just skip it
}

// Try to load user-specific config:
try {
    // Read some configs from a file:
    let rawdata = fs.readFileSync('./config.json');
    let userconfig = JSON.parse(rawdata);

    // override default configs:
    for (prop in userconfig) {
        config[prop] = userconfig[prop];
    }
}
catch (err) {
    // cannot load user config, just skip it
}

// Override default config and user config if environment variable is set:
if (process.env.PORT) {
    config.port = process.env.PORT;
}

module.exports = config;

如上例所示。您可以使用任何逻辑来构造配置数据。如果任何配置数据来自异步源(例如数据库),您将需要更有创意,但您可以将整个模块包装在一个 Promise 中并等待配置。

一般来说,配置应该保存在文件中。不是环境变量。环境变量应该存储有关环境的信息,例如 C 编译器的路径在哪里,或者您的 Web 浏览器在哪里,或者这台计算机配置为默认使用什么语言。进程端口号之类的东西不应该存储在环境变量中,尽管传统上(我指的是从 1960 年代发展到今天的 Unix 传统)传统上可以接受使用环境变量来覆盖配置以帮助调试。

根据您使用的操作系统,人们传统上存储配置文件的位置有很多:/etcUnixApplication Data上的目录、Windows 上的~/.config目录、现代 unix 桌面上的目录等。

有几个 npm 模块可以帮助您组织配置文件。根据您的需要,您可能会发现rcconfigconfigstore有用。就个人而言,我想推广我自己的config-default-cjson模块,但上面的其他模块具有更多功能。


推荐阅读