首页 > 解决方案 > Mern heroku 重新加载页面时出现内部服务器错误

问题描述

当我重新加载 Heroku 页面时,我收到一个内部服务器错误,状态码为 500。
但它不会在我的本地环境中发生。
最重要的是,
当我访问正确重定向并正常显示的路由地址时。
当我重新加载页面时,在 chrom 开发工具的网络信息中说该页面没有处理任何进程。
在此处输入图像描述 我的程序有什么问题?这是网站。
https://boiling-falls-61617.herokuapp.com/

这是我的错误和代码。
错误

2020-03-15T19:33:53.875586+00:00 heroku[router]: at=info method=GET path="/dashboards/5e6b999eb7f157252c8c6937" host=boiling-falls-61617.herokuapp.com request_id=785b2a93-2739-455f-856e-e59fb5af18ce fwd="99.251.226.61" dyno=web.1 connect=0ms service=2ms status=500 bytes=404 protocol=https


服务器代码

if (process.env.NODE_ENV === "production") {
  app.use(express.static(path.join(__dirname, "client", "build")));
  app.get("*", (req, res) => {
    // don't serve api routes to react app
    res.sendFile(path.join(__dirname, "./client/build/index.html"));
  });
  console.log("Serving React App...");
}


前端

function App() {
  return (
    <MuiThemeProvider theme={theme}>
      <BrowserRouter>
        <PrivateRoute path='/dashboards/:dashboardId' component={DashBoard} />
        <Route exact path='/' component={SignIn} />
        <Route path='/signup' component={SignUp} />
        <PrivateRoute
          path='/(dashboards|calendar)/:dashboardId/columns/:columnId/tasks/:taskId'
          render={props => <CardModal {...props} />}
        />
        <Route path='/calendar/:dashboardId' component={Calendar} />
      </BrowserRouter>
    </MuiThemeProvider>
  );
}

标签: reactjsexpressheroku

解决方案


我遇到了类似的问题;在本地工作,而不是在 Heroku。对我来说,解决方法是:

  • 我必须明确添加const path = require("path");到我的 server.js 文件中。

添加后,它似乎对我有用。

您还可以查看 heroku 日志以获取有关服务器错误的更多详细信息,方法是转到您的 heroku 帐户并单击应用程序,然后单击导航栏中的按钮并选择“查看日志”


推荐阅读