[英]How can I get my MERN app to refresh correctly on heroku?
I deployed my MERN app to heroku.我将我的 MERN 应用程序部署到 heroku。 It loads, and I can navigate between pages by clicking on the nav bar.它加载,我可以通过单击导航栏在页面之间导航。 But, if I refresh a page, it just comes up blank.但是,如果我刷新一个页面,它就会出现空白。 There are no error messages on the screen or in the console.屏幕或控制台上没有错误消息。 If I go back to a previous page that worked before, it also comes up blank.如果我 go 回到以前工作的上一页,它也会出现空白。 At this point, I don't know what to look for.在这一点上,我不知道要寻找什么。
My problem stemmed from incorrectly defining the root react route in my server.js file.我的问题源于在我的 server.js 文件中错误地定义了根反应路由。 I got the home page to render and refresh, but then the app would not load any other pages.我让主页呈现和刷新,但应用程序不会加载任何其他页面。 Here is the server.js file at that point:这是此时的 server.js 文件:
const express = require("express");
const mongoose = require("mongoose");
const routes = require("./routes");
const app = express();
// set the port for mongo connection to 3001 in development mode
const PORT = process.env.PORT || 3001;
// Configure body parsing for AJAX requests
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// Serve up static assets
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
app.get("*", function (req, res) {
res.sendFile(path.join(__dirname, "./client/build/index.html"));
});
}
app.use(routes);
mongoose.connect(
process.env.MONGODB_URI || "mongodb://localhost/portfolio_db",
{
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
}
);
// Start the API server on port 3001
app.listen(PORT, () =>
console.log(`🌎 ==> API Server now listening on PORT ${PORT}!`)
);
This sort of fixed my problem, but the app still wasn't routing properly, so I posted this: new issue这种解决了我的问题,但应用程序仍然没有正确路由,所以我发布了这个: 新问题
Basically, there where 3 problems:基本上,有3个问题:
I also deleted a statement defining the default react route in /routes/index.js.我还删除了 /routes/index.js 中定义默认反应路由的语句。 Now the app deploys and routes correctly.现在应用程序正确部署和路由。
I was also getting same error "cannot get /page" whenever I did refresh or window.location() So after 3 days of trials and deploys I found this solution.每当我刷新或 window.location() 时,我也会收到相同的错误“无法获取 /page”所以经过 3 天的试验和部署,我找到了这个解决方案。 Add this in your server.js file before deploying.在部署之前将其添加到您的 server.js 文件中。
if(process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
app.get("/*", function(req, res) {
res.sendFile(path.join(__dirname, "./client/build/index.html"));
}); }
You can add this code snippet before app.listen.您可以在 app.listen 之前添加此代码段。 Remember the path says./client/build/index.html which references to your build file in the production build in remote heroku master.记住路径说 ./client/build/index.html 它引用了远程 heroku 主控中生产构建中的构建文件。
cannot get /page happens because the server is not able to find the route which was created using react-router or anything else in frontend so we need to direct the app to index.html in production build so that it starts the whole frontend process again and identifies the route...无法获取 /page 发生,因为服务器无法找到使用 react-router 或前端中的其他任何内容创建的路由,因此我们需要将应用程序定向到 index.html 在生产构建中,以便它再次启动整个前端过程并识别路线...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.