node.js 如何在heroku上为节点应用程序设置环境变量并连接到postgresql数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40119964/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:46:33  来源:igfitidea点击:

How to set environment variables on heroku for nodes app and connect to the postgresql database?

node.jsherokuenvironment-variablesconfig

提问by javascript2016

I know there are same/similar questions on stack overflow and I have read the documentation too-I just still don't understand ANYTHING- how to set those variables and WHERE!! to set them.

我知道关于堆栈溢出有相同/类似的问题,我也阅读了文档-我仍然什么都不明白-如何设置这些变量以及在哪里!!来设置它们。

My env/production.js file:

我的 env/production.js 文件:

module.exports = {
    "DATABASE_URI": process.env.DATABASE_URI,
    "SESSION_SECRET": process.env.SESSION_SECRET,
    "TWITTER": {
       "consumerKey": process.env.TWITTER_CONSUMER_KEY,
       "consumerSecret": process.env.TWITTER_CONSUMER_SECRET,
       "callbackUrl": process.env.TWITTER_CALLBACK
    },
    "FACEBOOK": {
        "clientID": process.env.FACEBOOK_APP_ID,
        "clientSecret": process.env.FACEBOOK_CLIENT_SECRET,
        "callbackURL": process.env.FACEBOOK_CALLBACK_URL
    },
    "GOOGLE": {
        "clientID": process.env.GOOGLE_CLIENT_ID,
        "clientSecret": process.env.GOOGLE_CLIENT_SECRET,
        "callbackURL": process.env.GOOGLE_CALLBACK_URL
    },
    "LOGGING": true
};

In my env/development.js file I set the variables (linked to my postgres/localhost/xxx). Do I need to set them in heroku for all (Google, Facebook etc.) or just for the database, since I had to create one with heroku? Do I leave the link to my local database in my development file and link to the heroku database separately?

在我的 env/development.js 文件中,我设置了变量(链接到我的 postgres/localhost/xxx)。我是否需要在 heroku 中为所有人(谷歌、Facebook 等)或仅为数据库设置它们,因为我必须用 heroku 创建一个?我是否将指向本地数据库的链接保留在我的开发文件中并分别链接到 heroku 数据库?

I don't even know if I am suppose to do it from my command line?! In the documentation it says: heroku config:get CONFIG-VAR-NAME -s >> .env so would it be heroku config:get CONFIG-NAME OF MY HEROKU DATABASE -s >> .env?

我什至不知道我是否应该从命令行执行此操作?!在文档中它说:heroku config:get CONFIG-VAR-NAME -s >> .env 那么它会是 heroku config:get CONFIG-NAME OF MY HEROKU DATABASE -s >> .env 吗?

I'm deploying for the first time and so confused! Help :)

我是第一次部署,很困惑!帮助 :)

回答by dark_ruby

According to documentationyou could use heroku CLI

根据您可以使用的文档heroku CLI

$ heroku config:set DATABASE_URI=database_uri_here
$ heroku config:set SESSION_SECRET=session_secret
... and so on for each variable, 

or you could use UI https://dashboard-classic.heroku.com/apps/{your-app-name}/settings and provide same variables via web interface, as I mention in above comment

或者你可以使用 UI https://dashboard-classic.heroku.com/apps/{your-app-name}/settings 并通过网络界面提供相同的变量,正如我在上面的评论中提到的

NODE_ENV=productionis not treated specially by heroku, so you do need to provide it as well as any other env variable

NODE_ENV=production没有被 特殊对待heroku,因此您确实需要提供它以及任何其他环境变量

ps: strictly speaking this question doesnt really belong in SO, as it's unrelated to programming. Maybe it need to be moved to SU

ps:严格来说这个问题并不真正属于SO,因为它与编程无关。也许它需要移动到SU

回答by Tay

Use:

用:

heroku config:set $(cat .env | sed '/^$/d; /#[[:print:]]*$/d')

to set your config from .env file.

从 .env 文件设置您的配置。

回答by Avtar Nanrey

Especially database connection variables will not work through Heroku env variables. I would suggest using dotenvand set variables to the file by installing Heroku CLIand then use bash:

特别是数据库连接变量不能通过 Heroku 环境变量工作。我建议使用dotenv并通过安装Heroku CLI为文件设置变量,然后使用 bash:

heroku login
heroku run bash -a app_name

create .envfile if it is not there and add values to it

.env如果文件不存在,则创建文件并向其添加值

touch .env
echo "ENV_VAR=value" >> .env

confirm the entry in a file

确认文件中的条目

cat .env