本文翻译自:Setting Environment Variables for Node to retrieve
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, 从磁盘上的JSON文件加载,
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
? 基于一些谷歌搜索,看来我需要在process.env
设置变量? How and where do I set these credentials? 如何以及在哪里设置这些凭据? Example Please. 请举例。
参考:https://stackoom.com/question/1VcXn/设置环境变量以供节点检索
It depends on your operating system and your shell 这取决于您的操作系统和外壳
On linux with the shell bash , you create environment variables like this(in the console): 在具有shell bash的linux上 ,您可以在控制台中创建如下这样的环境变量:
export FOO=bar
For more information on environment variables on ubuntu (for example): 有关ubuntu上的环境变量的更多信息(例如):
Environment variables on ubuntu Ubuntu上的环境变量
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. USER_ID
和USER_KEY
都可以分别从process.env.USER_ID
和process.env.USER_KEY
访问。 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. 看起来他们只是在为您USER_KEY
从process.env
或磁盘上某些特定文件加载USER_ID
和USER_KEY
之间进行选择的选择。
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
. 这将传递用户ID 239482
和用户密钥foobar
。 This is suitable for testing, however for production, you will probably be configuring some bash scripts to export variables. 这适用于测试,但是对于生产而言,您可能会配置一些bash脚本以导出变量。
只需在命令行上提供环境值
USER_ID='abc' USER_KEY='def' node app.js
For windows users this Stack Overflow question and top answer is quite useful on how to set environement variables via the command line 对于Windows用户,此Stack Overflow问题和最佳答案对于如何通过命令行设置环境变量非常有用。
How can i set NODE_ENV=production in Windows? 如何在Windows中设置NODE_ENV = production?
If you want a management option, try the envs npm package. 如果需要管理选项,请尝试使用envs npm软件包。 It returns environment values if they are set. 如果设置了环境值,它将返回。 Otherwise, you can specify a default value that is stored in a global defaults object variable if it is not in your environment. 否则,您可以指定默认值,如果该默认值不在您的环境中,则该默认值存储在全局默认对象变量中。
Using .env ("dot ee-en-vee") or environment files is good for many reasons. 使用.env (“ dot ee-en-vee”)或环境文件有很多好处。 Individuals may manage their own configs. 个人可以管理自己的配置。 You can deploy different environments (dev, stage, prod) to cloud services with their own environment settings. 您可以使用自己的环境设置将不同的环境(开发,阶段,产品)部署到云服务。 And you can set sensible defaults. 您可以设置合理的默认值。
Inside your .env
file each line is an entry, like this example: 在您的.env
文件中,每一行都是一个条目,例如以下示例:
NODE_ENV=development
API_URL=http://api.domain.com
TRANSLATION_API_URL=/translations/
GA_UA=987654321-0
NEW_RELIC_KEY=hi-mom
SOME_TOKEN=asdfasdfasdf
SOME_OTHER_TOKEN=zxcvzxcvzxcv
You should not include the .env
in your version control repository (add it to your .gitignore
file). 你不应该包括.env
在版本控制库(它添加到你.gitignore
文件)。
To get variables from the .env
file into your environment, you can use a bash script to do the equivalent of export NODE_ENV=development
right before you start your application. 要将变量从.env
文件获取到您的环境中,可以在启动应用程序之前使用bash脚本执行相当于export NODE_ENV=development
。
#!/bin/bash
while read line; do export "$line";
done
Then this goes in your application javascript: 然后这进入您的应用程序javascript:
var envs = require('envs');
// If NODE_ENV is not set,
// then this application will assume it's prod by default.
app.set('environment', envs('NODE_ENV', 'production'));
// Usage examples:
app.set('ga_account', envs('GA_UA'));
app.set('nr_browser_key', envs('NEW_RELIC_BROWSER_KEY'));
app.set('other', envs('SOME_OTHER_TOKEN));