Heroku 部署不适用于 postgresql api

问题描述

我正在研究在 https://www.taniarascia.com/node-express-postgresql-heroku/ 上找到的 nodejs api 它使用 postgresql,因此它要求您将 postgresql 附加组件下载到我在 heroku 女巫上的应用程序中,但是当我打开该应用程序时,它说无法获取 / 像预期的那样,但我转到 /books 并且它给了我 heroku 错误.当我这样做时:

heroku logs -t -a node-api-with-books

它给了我这个错误

2021-03-03T03:25:31.515099+00:00 app[web.1]: code: 'ECONNREFUSED',2021-03-03T03:25:31.515099+00:00 app[web.1]: syscall: 'connect',2021-03-03T03:25:31.515099+00:00 app[web.1]: address: '127.0.0.1',2021-03-03T03:25:31.515100+00:00 app[web.1]: port: 5432
2021-03-03T03:25:31.515100+00:00 app[web.1]: }
2021-03-03T03:25:31.542497+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/books" host=node-api-with-books.herokuapp.com request_id=83db8d38-c206-484a-8bca-a273ed8be8bd fwd="66.7.125.54" dyno=web.1 connect=5002ms service=95ms status=503 bytes=0 protocol=https
2021-03-03T03:25:31.578446+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-03-03T03:25:31.579040+00:00 app[web.1]: npm ERR! errno 1
2021-03-03T03:25:31.653173+00:00 app[web.1]: npm ERR! working-rest-api@1.0.0 start: `node index.js`
2021-03-03T03:25:31.653743+00:00 app[web.1]: npm ERR! Exit status 1
2021-03-03T03:25:31.656417+00:00 app[web.1]: npm ERR!
2021-03-03T03:25:31.656729+00:00 app[web.1]: npm ERR! Failed at the working-rest-api@1.0.0 start script.
2021-03-03T03:25:31.657018+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-03-03T03:25:31.693198+00:00 app[web.1]: 
2021-03-03T03:25:31.700905+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-03-03T03:25:31.701084+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2021-03-03T03_25_31_668Z-debug.log
2021-03-03T03:25:31.868076+00:00 heroku[web.1]: Process exited with status 1
2021-03-03T03:25:32.005535+00:00 heroku[web.1]: State changed from up to crashed
2021-03-03T03:25:32.847159+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=node-api-with-books.herokuapp.com request_id=d669a8bb-5c76-4007-8e20-2323df702196 fwd="66.7.125.54" dyno=web.1 connect=1ms service= status=503 bytes= protocol=https
2021-03-03T03:57:13.968272+00:00 heroku[web.1]: State changed from crashed to starting
2021-03-03T03:57:19.191432+00:00 heroku[web.1]: Starting process with command `npm start`
2021-03-03T03:57:23.003539+00:00 app[web.1]: 
2021-03-03T03:57:23.003564+00:00 app[web.1]: > working-rest-api@1.0.0 start /app
2021-03-03T03:57:23.003565+00:00 app[web.1]: > node index.js
2021-03-03T03:57:23.003565+00:00 app[web.1]: 
2021-03-03T03:57:23.514091+00:00 app[web.1]: Server listening
2021-03-03T03:57:23.596725+00:00 heroku[web.1]: State changed from starting to up
2021-03-03T04:31:59.242314+00:00 heroku[web.1]: Idling
2021-03-03T04:31:59.270196+00:00 heroku[web.1]: State changed from up to down
2021-03-03T04:32:00.860211+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2021-03-03T04:32:01.033002+00:00 heroku[web.1]: Process exited with status 143
2021-03-04T23:41:06.257484+00:00 heroku[web.1]: Unidling
2021-03-04T23:41:06.279151+00:00 heroku[web.1]: State changed from down to starting
2021-03-04T23:41:09.533736+00:00 heroku[web.1]: Starting process with command `npm start`
2021-03-04T23:41:13.424801+00:00 app[web.1]: 
2021-03-04T23:41:13.424844+00:00 app[web.1]: > working-rest-api@1.0.0 start /app
2021-03-04T23:41:13.424844+00:00 app[web.1]: > node index.js
2021-03-04T23:41:13.424844+00:00 app[web.1]: 
2021-03-04T23:41:13.856750+00:00 app[web.1]: Server listening
2021-03-04T23:41:14.426074+00:00 heroku[web.1]: State changed from starting to up
2021-03-04T23:41:15.345417+00:00 heroku[router]: at=info method=GET path="/" host=node-api-with-books.herokuapp.com request_id=1fafdcec-9742-41d6-a530-a15755f7f0cc fwd="66.7.125.54" dyno=web.1 connect=1ms service=11ms status=404 bytes=415 protocol=https
2021-03-04T23:41:20.464200+00:00 app[web.1]: /app/index.js:12
2021-03-04T23:41:20.464217+00:00 app[web.1]: throw err;
2021-03-04T23:41:20.464218+00:00 app[web.1]: ^
2021-03-04T23:41:20.464218+00:00 app[web.1]: 
2021-03-04T23:41:20.464219+00:00 app[web.1]: Error: connect ECONNREFUSED 127.0.0.1:5432
2021-03-04T23:41:20.464222+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
2021-03-04T23:41:20.464223+00:00 app[web.1]: errno: -111,2021-03-04T23:41:20.464223+00:00 app[web.1]: code: 'ECONNREFUSED',2021-03-04T23:41:20.464223+00:00 app[web.1]: syscall: 'connect',2021-03-04T23:41:20.464224+00:00 app[web.1]: address: '127.0.0.1',2021-03-04T23:41:20.464224+00:00 app[web.1]: port: 5432
2021-03-04T23:41:20.464225+00:00 app[web.1]: }
2021-03-04T23:41:20.477571+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/books" host=node-api-with-books.herokuapp.com request_id=24e2da67-be91-4e17-89b3-debef869bba4 fwd="66.7.125.54" dyno=web.1 connect=1ms service=49ms status=503 bytes=0 protocol=https
2021-03-04T23:41:20.484562+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-03-04T23:41:20.485036+00:00 app[web.1]: npm ERR! errno 1
2021-03-04T23:41:20.493801+00:00 app[web.1]: npm ERR! working-rest-api@1.0.0 start: `node index.js`
2021-03-04T23:41:20.494105+00:00 app[web.1]: npm ERR! Exit status 1
2021-03-04T23:41:20.494432+00:00 app[web.1]: npm ERR!
2021-03-04T23:41:20.494669+00:00 app[web.1]: npm ERR! Failed at the working-rest-api@1.0.0 start script.
2021-03-04T23:41:20.494947+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-03-04T23:41:20.511921+00:00 app[web.1]: 
2021-03-04T23:41:20.512219+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-03-04T23:41:20.512381+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2021-03-04T23_41_20_495Z-debug.log
2021-03-04T23:41:20.920060+00:00 heroku[web.1]: Process exited with status 1
2021-03-04T23:41:20.981301+00:00 heroku[web.1]: State changed from up to crashed
2021-03-04T23:41:21.001047+00:00 heroku[web.1]: State changed from crashed to starting
2021-03-04T23:41:24.349422+00:00 heroku[web.1]: Starting process with command `npm start`
2021-03-04T23:41:28.760560+00:00 app[web.1]: 
2021-03-04T23:41:28.760604+00:00 app[web.1]: > working-rest-api@1.0.0 start /app
2021-03-04T23:41:28.760605+00:00 app[web.1]: > node index.js
2021-03-04T23:41:28.760605+00:00 app[web.1]: 
2021-03-04T23:41:29.460226+00:00 app[web.1]: Server listening
2021-03-04T23:41:29.767594+00:00 heroku[web.1]: State changed from starting to up
2021-03-04T23:41:30.782783+00:00 app[web.1]: /app/index.js:12
2021-03-04T23:41:30.782799+00:00 app[web.1]: throw err;
2021-03-04T23:41:30.782800+00:00 app[web.1]: ^
2021-03-04T23:41:30.782800+00:00 app[web.1]: 
2021-03-04T23:41:30.782801+00:00 app[web.1]: Error: connect ECONNREFUSED 127.0.0.1:5432
2021-03-04T23:41:30.782802+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16) {
2021-03-04T23:41:30.782802+00:00 app[web.1]: errno: -111,2021-03-04T23:41:30.782803+00:00 app[web.1]: code: 'ECONNREFUSED',2021-03-04T23:41:30.782803+00:00 app[web.1]: syscall: 'connect',2021-03-04T23:41:30.782803+00:00 app[web.1]: address: '127.0.0.1',2021-03-04T23:41:30.782804+00:00 app[web.1]: port: 5432
2021-03-04T23:41:30.782804+00:00 app[web.1]: }
2021-03-04T23:41:30.806119+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/books" host=node-api-with-books.herokuapp.com request_id=0476a454-a185-465c-9df3-f5e0adf6ec8f fwd="66.7.125.54" dyno=web.1 connect=1ms service=143ms status=503 bytes=0 protocol=https
2021-03-04T23:41:30.897859+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2021-03-04T23:41:30.901791+00:00 app[web.1]: npm ERR! errno 1
2021-03-04T23:41:30.940174+00:00 app[web.1]: npm ERR! working-rest-api@1.0.0 start: `node index.js`
2021-03-04T23:41:30.941393+00:00 app[web.1]: npm ERR! Exit status 1
2021-03-04T23:41:30.942380+00:00 app[web.1]: npm ERR!
2021-03-04T23:41:30.942813+00:00 app[web.1]: npm ERR! Failed at the working-rest-api@1.0.0 start script.
2021-03-04T23:41:30.943425+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2021-03-04T23:41:31.316808+00:00 app[web.1]: 
2021-03-04T23:41:31.316994+00:00 app[web.1]: npm ERR! A complete log of this run can be found in:
2021-03-04T23:41:31.317190+00:00 app[web.1]: npm ERR!     /app/.npm/_logs/2021-03-04T23_41_30_947Z-debug.log
2021-03-04T23:41:31.419624+00:00 heroku[web.1]: Process exited with status 1
2021-03-04T23:41:31.527952+00:00 heroku[web.1]: State changed from up to crashed
2021-03-04T23:41:32.128408+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=node-api-with-books.herokuapp.com request_id=7909f853-52aa-43dc-a228-3190e28b31b9 fwd="66.7.125.54" dyno=web.1 connect=0ms service= status=503 bytes= protocol=https

是否有任何我应该关注的部分以及如何使其发挥作用?

您可能需要查看此内容以更好地理解https://www.taniarascia.com/node-express-postgresql-heroku/

解决方法

您正在尝试从 heroku 连接到本地 postgreSQL 数据库,尝试在 heroku 仪表板中设置一个新的环境变量,如下所示:

NODE_ENV=production

您需要设置所有环境。带有附加值的 var,如下所示:

DB_USER=[your-add-on-user]
DB_PASSWORD=[your-add-on-pass]
DB_HOST=[your-add-on-host]
DB_PORT=5432
DB_DATABASE=[your-add-on-database]

来源:https://www.taniarascia.com/node-express-postgresql-heroku/#environment-variables