Node.js:设置环境变量 [英] Node.js: Setting Environment Variables
问题描述
我有几种方法来加载凭据。
- 从环境变量加载
- 从磁盘上的JSON文件加载
密钥需要如下:
USER_ID,USER_KEY
...这意味着如果您正确设置了环境变量,则无需在应用程序中管理凭据。
根据一些Google搜索,看来我需要在 process.env
中设置变量?如何和在哪里设置这些凭据?示例请。
环境变量(在这种情况下)正用于将凭据传递给您的应用程序。 USER_ID
和 USER_KEY
都可以从 process.env.USER_ID
和 process.env.USER_KEY
。您不需要编辑它们,只需访问其内容。
看起来他们只是在加载 USER_ID
和 USER_KEY
从 process.env
或磁盘上的某个特定文件。
现在,运行应用程序时会发生魔法。
USER_ID = 239482 USER_KEY = foobar node app.js
这将传递用户ID 239482
和用户密钥为 foobar
。这适用于测试,但是对于生产,您可能会配置一些bash脚本来导出变量。
I'm trying to follow a tutorial and it says:
"There are a few ways to load credentials.
- Loaded from environment variables,
- Loaded from a JSON file on disk,
The keys need to be as follows:
USER_ID, USER_KEY
...This means that if you properly set your environment variables, you do not need to manage credentials in your application at all."
Based on some Googling, it appears that I need to set the variables in process.env
? How and where do I set these credentials? Example Please.
Environment variables (in this case) are being used to pass credentials to your application. USER_ID
and USER_KEY
can both be accessed from process.env.USER_ID
and process.env.USER_KEY
respectively. You don't need to edit them, just access their contents.
It looks like they are simply giving you the choice between loading your USER_ID
and USER_KEY
from either process.env
or some specificed file on disk.
Now, the magic happens when you run the application.
USER_ID=239482 USER_KEY=foobar node app.js
That will pass the user id 239482
and the user key as foobar
. This is suitable for testing, however for production, you will probably be configuring some bash scripts to export variables.
这篇关于Node.js:设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!