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

微笑、不失礼 提交于 2020-05-09 19:14:11

问题


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:

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?

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?

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


回答1:


According to documentation you could use 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

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

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




回答2:


Use:

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

to set your config from .env file.




回答3:


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

heroku login
heroku run bash -a app_name

create .env file if it is not there and add values to it

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

confirm the entry in a file

cat .env


来源:https://stackoverflow.com/questions/40119964/how-to-set-environment-variables-on-heroku-for-nodes-app-and-connect-to-the-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!