简体   繁体   English

如何让我的 MERN 应用程序在 heroku 上正确刷新?

[英]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个问题:

  1. 'path' wasn't required at the top of the file, so the path to index.html wasn't defined (had to look at heroku logs to see this error).文件顶部不需要“路径”,因此未定义 index.html 的路径(必须查看 heroku 日志才能看到此错误)。
  2. The default react route (app.get('*'....)) should not be in the if statement, and默认的反应路由 (app.get('*'....)) 不应该在 if 语句中,并且
  3. The default react route statement needed to be below the 'app.use(routes)' statement.默认的 react route 语句需要位于 'app.use(routes)' 语句的下方。

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.

相关问题 我如何正确使用我的 MERN APP 的 res.sendFile,我每次刷新它时都会出错 - How can I properly use the res.sendFile for my MERN APP, I get an error every time I refresh it MERN && Heroku:我的应用程序显示在 VirtualBox/Linux 环境中,但不在 Windows 或移动设备上 - MERN && Heroku: My app displays on VirtualBox/Linux environment, but not on Windows or mobile 将我的 MERN 项目部署到 heroku 后“无法获取 /” - "Cannot GET /" after deploying my MERN project to heroku MERN 无法部署到 heroku - MERN can't deploy to heroku 我怎样才能让我的每个模态正确显示? - How can I get each of my modals to show correctly? 我可以在哪里存储和检索用户在我的 MERN 应用中上传的音频文件? - Where can I store and retrieve audio files that the users upload in my MERN app? 如何使用 Auth0 和带有 MERN 应用程序的护照获取附加到 req object 的 session 属性? - How do I get the session property appended to the req object using Auth0 and passport with a MERN app? 无法让我的heroku应用程序的Mongo部分正常工作 - Can't get the Mongo portion of my heroku app to work 在 heroku MERN 应用程序中部署后的应用程序错误 - Application Error after deploy in heroku MERN app MERN 应用程序 on.netlify 和 heroku (CORS) 的问题 - Problem with MERN app on netlify and heroku (CORS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM