简体   繁体   English

如何在 Heroku 上为 Node 应用程序设置环境变量并连接到 PostgreSQL 数据库?

[英]How to set environment variables on Heroku for Node app and connect to the PostgreSQL database?

I know there are the 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 PostgreSQL/localhost/xxx).在我的 env/development.js 文件中,我设置了变量(链接到我的 PostgreSQL/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?我是否需要在 Heroku 中为所有人(谷歌、Facebook 等)或仅为数据库设置它们,因为我必须用 Heroku 创建一个? Do I leave the link to my local database in my development file and link to the Heroku database separately?我是否将指向本地数据库的链接保留在我的开发文件中并分别链接到 Heroku 数据库?

I don't even know if I am supposed 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 :)帮助 :)

According to documentation you 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=production is 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. ps:严格来说这个问题并不真正属于SO,因为它与编程无关。 Maybe it need to be moved to SU也许它需要移动到SU

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

从 .env 文件设置您的配置

Especially database connection variables will not work through Heroku env variables.特别是数据库连接变量不能通过 Heroku 环境变量工作。 I would suggest using dotenv and set variables to the file by installing Heroku CLI and then use bash:我建议使用dotenv并通过安装Heroku CLI为文件设置变量,然后使用 bash:

heroku login
heroku run bash -a app_name

create .env file 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

Heroku now automatically sets the DATABASE_URL environment variable when you add the PostgreSQL add-on Heroku 现在会在您添加 PostgreSQL 附加组件时自动设置DATABASE_URL环境变量

This variable can then be seen just next to the other environment variables, possibly manually defined, under:然后可以在其他环境变量旁边看到此变量,可能是手动定义的,位于:

  • Settings设置
  • Config Vars配置变量

That added variable already contains secret authentication information, so you should just use it as is.添加的变量已经包含秘密身份验证信息,因此您应该按原样使用它。

How to get it working with Sequelize如何让它与 Sequelize 一起工作

I just managed a working solution with Sequelize as mentioned at: Deploy FeathersJS App on Heroku with Sequelize, you basically just need to do:我刚刚使用 Sequelize 管理了一个有效的解决方案,如:使用 Sequelize 在 Heroku部署 FeathersJS 应用程序,您基本上只需要执行以下操作:

sequelize = new Sequelize(process.env.DATABASE_URL, {
    dialect: 'postgres',
    protocol: 'postgres',
    dialectOptions: {
        ssl: {
            require: true,
            rejectUnauthorized: false
        }
    }
});

as mentioned at: Can't connect to heroku postgresql database from local node app with sequelize如上所述: 无法使用 sequelize 从本地节点应用程序连接到 heroku postgresql 数据库

Loggin in Heroku CLI:在 Heroku CLI 中登录:

heroku login

list all apps:列出所有应用程序:

heroku apps

Massive export of environment variables大量导出环境变量

cat .env | tr '\n' ' ' | xargs heroku config:set -a your_app

Another option:另外一个选项:

$ heroku login

then edit all config variables:然后编辑所有配置变量:

$ VISUAL="emacs" heroku config:edit -a <app_name>

This opens emacs where you can copy/paste the entire .env content.这将打开 emacs,您可以在其中复制/粘贴整个.env内容。 Then save and quit (Ctrl+x Ctrl+s, Ctrl+x Ctrl+c).然后保存并退出(Ctrl+x Ctrl+s、Ctrl+x Ctrl+c)。

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

相关问题 如何在 Heroku 应用程序中设置环境变量 - How can I set environment variables in a Heroku app 无法为 node.js 应用程序内的反应应用程序设置环境变量。 (heroku) - Cannot set environment variables for react app inside node.js app. (heroku) 无法使用 sequelize 从本地节点应用程序连接到 heroku postgresql 数据库 - Can't connect to heroku postgresql database from local node app with sequelize 如何从 Express 连接到 Heroku PostgresQL 数据库? - How to connect to Heroku PostgresQL database from Express? 在Node JS中根据环境变量配置多个postgresql数据库 - Configure multiple postgresql database according to environment variables in Node JS 如何使用Node设置环境变量? - How to set environment variables using Node? 应用程序上传到 heroku 后如何连接到数据库 - How to connect to database once app is uploaded to heroku 尝试将我的node.js连接到Heroku PostgreSQL数据库。 关注Heroku Postgres教程 - Trying to connect my node.js to Heroku PostgreSQL database. Following Heroku Postgres tutorial 如何将我的本地 node.js 应用程序连接到我的 Heroku Postgres 数据库? - How can I connect my local node.js app to my Heroku Postgres database? 试图将我的 Node.js 应用程序连接到 Heroku PostgreSQL DB。 努力连接到数据库 - Trying to connect my Node.js app to Heroku PostgreSQL DB. Struggling to connect to the DB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM