React-Heroku Postgress-Addon:GET http://app-name.herokuapp.com/any 503(服务不可用)
Posted
技术标签:
【中文标题】React-Heroku Postgress-Addon:GET http://app-name.herokuapp.com/any 503(服务不可用)【英文标题】:React-Heroku Postgress-Addon: GET http://app-name.herokuapp.com/any 503 (Service Unavailable) 【发布时间】:2021-05-21 03:28:38 【问题描述】:我正在尝试将 CRUD 待办事项列表应用程序部署到 Heroku。我正在使用来自 heroku 的 React、NodeJS 和 Postgress 插件。在部署过程中我没有收到任何错误,但是前端似乎没有与后端连接。当我尝试添加新的“待办事项”时,我收到下一条消息(应用程序也无法从数据库中获取条目):
index.js
const express = require("express");
const app = express();
const cors = require("cors");
const pool = require("./db");
const path = require("path");
const PORT = process.env.PORT || 5000;
//middleware
app.use(cors());
app.use(express.json()); // => allows us to access the req.body
if (process.env.NODE_ENV === "production")
//server static content
//npm run build
app.use(express.static(path.join(__dirname, "client/build")));
console.log(__dirname);
console.log(path.join(__dirname, "client/build"));
//ROUTES//
//get all Todos
app.get("/todos", async (req, res) =>
try
const allTodos = await pool.query("SELECT * FROM todo");
res.json(allTodos.rows);
catch (err)
console.error(err.message);
);
//get a todo
app.get("/todos/:id", async (req, res) =>
try
const id = req.params;
const todo = await pool.query("SELECT * FROM todo WHERE todo_id = $1", [
id,
]);
res.json(todo.rows[0]);
catch (err)
console.error(err.message);
);
//create a todo
app.post("/todos", async (req, res) =>
try
console.log(req.body);
const description = req.body;
const newTodo = await pool.query(
"INSERT INTO todo (description) VALUES ($1) RETURNING *",
[description]
);
res.json(newTodo.rows[0]);
catch (err)
console.error(err.message);
);
//update a todo
app.put("/todos/:id", async (req, res) =>
try
const id = req.params;
const description = req.body;
const updateTodo = await pool.query(
"UPDATE todo SET description = $1 WHERE todo_id = $2",
[description, id]
);
res.json("Todo was updated");
catch (err)
console.error(err.message);
);
//delete a todo
app.delete("/todos/:id", async (req, res) =>
try
const id = req.params;
const deleteTodo = await pool.query("DELETE FROM todo WHERE todo_id = $1", [
id,
]);
res.json("Todo was deleted");
catch (err)
console.error(err.message);
);
app.get("*", (req, res) =>
res.sendFile(path.join(__dirname, "client/build/index.html"));
);
app.listen(PORT, () =>
console.log(`Server is starting on port $PORT`);
);
db.js
const Pool = require("pg").Pool;
require("dotenv").config();
const devConfig = `postgresql://$process.env.PG_USER:$process.env.PG_PASSWORD@$process.env.PG_HOST:$process.env.PG_PORT/$process.env.PG_DATABASE`;
const proConfig = process.env.DATABASE_URL; //heroku addons
const pool = new Pool(
connectionString:
process.env.NODE_ENV === "production" ? proConfig : devConfig,
);
module.exports = pool;
package.json(根目录)
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"engines":
"node": "10.14.2",
"npm": "6.14.2"
,
"scripts":
"start": "node index.js",
"heroku-postbuild": "cd client && npm install && npm run build"
,
"author": "henry",
"license": "ISC",
"dependencies":
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"pg": "^7.18.2"
package.json(客户端)
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies":
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-scripts": "3.4.0"
,
"scripts":
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
,
"eslintConfig":
"extends": "react-app"
,
"browserslist":
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
,
"proxy": "http://localhost:5000"
我正在使用以下 git 存储库中的代码:https://github.com/l0609890/pern-deploy-tutorial。 并按照下面的说明(我认为您可以将视频跳到最少 34:00):https://www.youtube.com/watch?v=ZJxUOOND5_A
非常感谢!
【问题讨论】:
2021-02-18T06:34:45.692018+00:00 app[web.1]: description: 'hi' 2021-02-18T06:34:45.927723+00:00 app[web.1]: no pg_hba.conf entry for host "35.175.243.252", user "arkqcuvolsgwnz", database "d2l3k93rvhp242", SSL off 2021-02-18T06:34:51.137702+00:00 heroku[router]: at=error code=H12 desc="Request timeout" method=POST path="/todos" host=todo-app-pern-stack.herokuapp.com request_id=caf16648-4ee5-444a-85f1-f845c183885e fwd="104.185.38.51" dyno=web.1 connect=2ms service=30001ms status=503 bytes=0 protocol=https
【参考方案1】:
由于 Heroku 使用 Heroku 管理的负载均衡器,因此超时设置为 30 秒。
如果在 catch 块中出现错误,您似乎还没有返回带有错误响应的函数。
请在 catch 块中发送响应以终止函数并避免超时错误
建议:
返回相应的状态码和相应的错误信息
【讨论】:
【参考方案2】:我也在关注那个教程。事实证明,您只需将 ssl
属性添加到您的 proConfig
const proConfig =
connectionString: process.env.DATABASE_URL,
ssl:
rejectUnauthorized: false
;
来源:Node.js, PostgreSQL error: no pg_hba.conf entry for host
【讨论】:
以上是关于React-Heroku Postgress-Addon:GET http://app-name.herokuapp.com/any 503(服务不可用)的主要内容,如果未能解决你的问题,请参考以下文章