简体   繁体   English

环境变量和检测环境 NodeJS/React

[英]Environment Variables and detecting environment NodeJS/React

I've looked into things like dotenv and get the concept of environment variables, the part I am missing is where and when the detection of which variable to use happens.我研究了dotenv之类的东西并获得了环境变量的概念,我缺少的部分是检测使用哪个变量的位置和时间。 To be specific I'm dealing with the Stripe API key and of course, I want to use the Test API key when I'm developing locally and then the Live API key when I push to production.具体来说,我正在处理 Stripe API 密钥,当然,我想在本地开发时使用 Test API 密钥,然后在推送到生产环境时使用 Live API 密钥。

So obviously I'll have a.env file with something like;所以很明显我会有一个类似的.env文件;

test_API_KEY=1234
live_API_KEY=5678

But then surely somewhere in my code I need something like但是肯定在我的代码中的某个地方我需要类似的东西

var keyToUse;
if(productionEnvironment){
     keyToUse = process.env.live_API_KEY
}
else if(!productionEnvironment){
     keyToUse = process.env.test_API_KEY
}

Or does like dotenv (or secure dot env ) manage that for you?还是像 dotenv (或安全的 dot env )一样为您管理? Or is this done with another tool/technique?或者这是用另一种工具/技术完成的?

Thanks in advance提前致谢

The dotenv recommends to use a same name of env variable regardless of deploy environment. dotenv建议无论部署环境如何,都使用相同名称的 env 变量。

For example,例如,

.env (in local) .env(本地)

API_KEY=local_api_key

.env (in test) .env(测试中)

API_KEY=test_api_key

The base principle says separation of config from code .基本原则separation of config from code (the .env file may be included in .gitignore ) .env文件可能包含在.gitignore中)

In your way, if some environment is added (like stage-2), some associated code may be added.以您的方式,如果添加了一些环境(如第 2 阶段),则可能会添加一些相关代码。


If you want to have a set of constant values by deploy environments as code, just create config.local.js , config.test.js .....如果您想通过将环境部署为代码来获得一组常量值,只需创建config.local.jsconfig.test.js .....

consider below code)考虑下面的代码)

let constSet;
switch(process.env.DEPLOY_ENV) {
   case 'local':
      constSet = require('./config.local')
      break; 
...
}

Ok after a bit more digesting, the problem actually comes from a fundamental misunderstanding of how to use Environment Variables.好吧,经过一番消化,问题实际上来自对如何使用环境变量的基本误解。 I was imagining the situation that live environment variables should be in the application (perhaps in a JSON file somewhere), and then used according to what environment the application was in.我在想象实时环境变量应该在应用程序中的情况(可能在某处的 JSON 文件中),然后根据应用程序所处的环境使用。

But it's actually the case (or at least it appears to me) that you need to set the environment variable, IN THE ENVIRONMENT - not the code, So for example on AWS or Google Cloud, those will then overwrite whatever you are using for local use as @getElementsByNature is alluding to with the same naming convention但实际上(或至少在我看来)您需要在环境中设置环境变量 - 而不是代码,因此例如在 AWS 或 Google Cloud 上,这些将覆盖您用于本地的任何内容使用 @getElementsByNature 暗示使用相同的命名约定

That way when you develop locally with API_KEY=1234 it gets overwritten on live with API_KEY=4567 and there is no (if environment == live), then do this...这样,当您使用 API_KEY=1234 在本地开发时,它会被 API_KEY=4567 实时覆盖并且没有(如果环境 == 实时),然后执行此操作...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM