使用Heroku部署时出现问题(错误504) [英] Issue when deploying with Heroku (Error 504)
问题描述
我正在部署一个非常小的React App + Node服务器来测试Heroku的部署,
I'm deploying a very small React App + Node server to test deployment with Heroku,
我可以获取客户端,但不能获取服务器
I can get the client but not the server,
回购: https://github.com/Versifiction/ofilms-test-heroku
网站: https://test-ofilms-heroku.herokuapp.com/
在控制台中,出现504错误
In the console, there's a 504 error
Failed to load resource: the server responded with a status of 504 (Gateway Timeout)
xhr.js:166 GET https://test-ofilms-heroku.herokuapp.com/api/message 504 (Gateway Timeout)
您知道我必须添加什么来纠正此504错误吗?
Do you know what I have to add to correct this 504 error ?
推荐答案
花点时间看一下您的仓库.不是setupProxy.js
,而是设置API的方式.
Got some time to take a look at your repo. It's not the setupProxy.js
, but instead it's how you've set up your API.
第一期:您正在尝试为开发中的生产资产提供服务.
First issue: You're trying to serve production assets in development.
app.use(express.static(path.join(__dirname, "build")));
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
修复:在生产中,如果请求的路由不匹配,它将捕获请求并回退到客户端index.html
.
Fix: When you're in production, it'll capture requests and fallback to the client index.html
if a requested route doesn't match.
if (process.env.NODE_ENV == "production") {
app.use(express.static(path.join(__dirname, "build")));
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
}
第二个问题:在提供API路由之前,您将上述资产作为通配符/
(更改为*
).换句话说,它永远不会到达您的GET
请求的API路由,因为/
(*
)首先捕获了所有GET
请求.
Second issue: You're serving the above assets as a wildcard /
(changed to *
) before you're serving your API routes. In other words, it'll never reach your GET
requested API routes since /
(*
) catches all GET
requests first.
修复:您的所有API路由都必须位于客户端生产版本之上-这样,请求首先流经API,然后再回退到客户端.
Fix: All of your API routes must sit above your client production build -- this way, requests flow through the API first and then fallback to the client second.
app.get("/api/message", (req, res) => {
res.json({ message: "Test déploiement d'O'Films sur Heroku" });
});
app.get("/api/hello", (req, res) => {
res.json({ message: "Hello world !" });
});
if (process.env.NODE_ENV == "production") {
app.use(express.static(path.join(__dirname, "build")));
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
}
app.listen(port, () => {
console.log(`Server is on up ${port} now !`);
});
工作示例存储库: https://github.com/mattcarlotta/公证人重构的
这篇关于使用Heroku部署时出现问题(错误504)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!