简体   繁体   English

在Heroku中设置默认环境变量

[英]Setting default environment variables in Heroku

I'm working on an app that connects to third-party APIs which require the use of an APP ID and SECRET KEY. 我正在开发一个连接到需要使用APP ID和SECRET KEY的第三方API的应用程序。

I am storing these values as environment variables in heroku, so that I don't need to expose them in my code. 我将这些值作为环境变量存储在heroku中,因此无需在代码中公开它们。

If I deploy to heroku, it will use heroku's environment variables to resolve these API credentials. 如果我部署到heroku,它将使用heroku的环境变量来解析这些API凭据。

If I'm working on it locally, I want to use my config.js module, and lookup the API credentials there. 如果我在本地处理它,那么我想使用config.js模块,并在那里查找API凭据。 NOTE: This config.js file is included in my .gitignore so that these credentials never end up in the cloud. 注意:此config.js文件包含在我的.gitignore因此这些凭据永远不会出现在云中。

The problematic code is this: 有问题的代码是这样的:

var api_secret = process.env.API_SECRET || require('../../config.js').secret;

When I run this locally, I've got no issues. 在本地运行时,没有任何问题。 Meaning, it is unable to resolve the environment variable, so instead it uses the secret from within config.js . 这意味着,它无法解析环境变量,因此它使用config.jssecret

When I run it on heroku, it DOES throw an error telling me that module 'config.js' could not be found . 当我在heroku上运行它时,它确实抛出错误,告诉我module 'config.js' could not be found This makes sense, because it was never pushed up with the rest of the repo, by virtue that it is in my .gitignore . 这是有道理的,因为由于它位于我的.gitignore ,因此它从未被回购的其余部分推高。

Because heroku is parsing through my code before it ever runs, the require('../../config.js') is problematic. 因为heroku在我的代码运行之前就对其进行了解析,所以require('../../config.js')有问题。 It is trying to lookup a file that doesn't exist. 它正在尝试查找不存在的文件。

How can I solve the issue of using environment variables when deployed, and the config.js module when running locally? 如何解决部署时使用环境变量以及本地运行时使用config.js模块的问题?

On the Heroku dashboard for your application, you can set config variables. 在您的应用程序的Heroku仪表板上,可以设置配置变量。 If you have the Heroku Toolbelt set up on your machine, you can also use: 如果您在计算机上设置了Heroku Toolbelt,则还可以使用:

heroku config:set API_SECRET=secret

See this article for more. 有关更多信息,请参见本文

Edit: Think I may have misunderstood the question. 编辑:认为我可能误解了这个问题。 I would suggest, if possible, using the dotenv npm package to set your config variables locally. 我建议,如果可能的话,请使用dotenv npm软件包在本地设置配置变量。

If not, another thing to check would be that the config.js package is in your package.json file, because Heroku will use this to build your dependencies. 如果不是,则要检查的另一件事是config.js包位于package.json文件中,因为Heroku将使用它来构建依赖项。

If you do not want to push your config.js to heroky at all, you can just follow the following to determine whether the config file exists or not with a try catch and the file system module: 如果您根本不想将config.js推为英雄,则可以按照以下步骤使用try catch和文件系统模块来确定配置文件是否存在:

Check synchronously if file/directory exists in Node.js 同步检查Node.js中是否存在文件/目录

In your case: 在您的情况下:

var fs = require('fs'),
    api_secret,
    config;

try {
    // Check whether config.js exists
    config = fs.lstatSync('../../config.js');
    // If we reach this line then config.js exists, yay!
    api_secret = process.env.API_SECRET || require('../../config.js').secret;
    // or alternatively  api_secret = require('../../config.js').secret; 
    // depending on your logic

}
catch (e) {
    // else config.js does not exist
    api_secret = process.env.API_SECRET
}

To run Heroku commands programmatically, you can set up a free Ruby app and make it do what you want through API calls. 要以编程方式运行Heroku命令,您可以设置一个免费的Ruby应用程序,并通过API调用使其执行所需的操作。 Use Heroku-api. 使用Heroku-api。 See https://github.com/heroku/heroku.rb 参见https://github.com/heroku/heroku.rb

If you want to set Heroku commands manually, you can set env variables on Heroku either with the command heroku config:set MYVAR=MYVALUE or through the Heroku dashboard (Click on your app > settings > reveal config vars > edit). 如果要手动设置Heroku命令,则可以使用命令heroku config:set MYVAR=MYVALUE或通过Heroku仪表板在Heroku上设置环境变量(单击应用程序>设置>显示配置变量>编辑)。

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

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