我的问题与中概述的问题即使不完全相同,也非常相似this https://stackoverflow.com/questions/41772411/react-routing-works-in-local-machine-but-not-heroku问题。不幸的是,我无法使用它提供的策略来解决它。所以这是我自己的详细信息:
我在用创建反应应用程序, 反应路由器 4, Express, and Heroku并已按照说明进行操作here https://github.com/fullstackreact/food-lookup-demo#deploying关于使用 CRA 设置服务器。
在本地,我可以访问诸如myapp/about
,但是在构建并推送到 Heroku 后,这些 404.
我可以通过 UI 导航到该路线(即通过单击将路线推送到的菜单项)history
),但仅使用浏览器的地址栏无法导航到此路线。此外,当我使用 UI 导航时,我没有看到任何与路线相关的网络活动,例如/about
要求。然而,当我更改地址栏并按 Enter 键时,这会产生对所述路由的网络请求。
以下是我的代码中的一些精选片段:
app.js
<Switch>
<Route exact path="/about" component={About} />
<Route path="/"
render={props => <coolListContainer {...props}/>} />
</Switch>
服务器.js
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
}
//...what some of my api routes look like:
app.route('/api/verify')
.post( async (req, res) => {
try {
await db.verifyCode(req.body)
res.json('success')
} catch (err) {
res.json(err);
}
}
});
我的目录结构由 full-stack-react 提供快速演示 https://github.com/fullstackreact/food-lookup-demo.
└── myapp
├── Procfile
├── README.md
├── client
│ ├── build
│ │ ├── asset-manifest.json
│ │ ├── index.html
│ │ └── static
│ │ ├── css
│ │ │ ├── main.e8c50ca0.css
│ │ │ └── main.e8c50ca0.css.map
│ │ └── js
│ │ ├── main.24fe0ebe.js
│ │ └── main.24fe0ebe.js.map
│ ├── package.json
│ ├── public
│ │ └── index.html
│ ├── src
│ │ ├── About.js
│ │ └── index.js
│ └── styles
│ └── about..css
├── package.json
├── server.js
└── static.json
根据给出的答案这个帖子 https://stackoverflow.com/questions/41772411/react-routing-works-in-local-machine-but-not-heroku,我还扑通一声static.json
文件放入根目录。
静态.json
{
"root": "client/build/",
"clean_urls": false,
"routes": {
"/**": "index.html"
}
}
上面的配置在任何路由上都会给我 404 错误。