如何使用带有快速路由器的react-router?

时间:2018-02-17 13:36:41

标签: reactjs express react-router react-redux react-router-v4

我的项目包含客户端服务器文件夹

客户:我已经做出反应还原反应路由器应用,端口:localhost:3000

服务器:我有Express.Router()的快递服务器 和我的应用程序捆绑了来自客户端的webpack和这个应用程序可用于'/'端口:localhost:3001

当我在 CLIENT(端口:3000)文件夹中启动webpack-dev-server时,每条路由都可以正常运行,然后我将启动带有nodemon SERVER(端口:3001)的快速服务器并面临此问题:

我遇到的问题是当我在 SERVER(端口:3001)上执行任何路由时,除了'/' 我正在通过Express收到404错误。 所以任何人都可以解释我如何一起使用 react-router和express ,所以我在捆绑应用程序中的所有路由都可以在开发时在webpack-dev-server中工作

例如,我们有2条路线:

  <Route exact path="/" component={Home} />
  <Route path="/test" component={Test} />

我们有server.js和express app:

 app.get('*', (req,res) => 
 res.sendFile(path.join(__dirname+'/public/index.html')))

当我收到'localhost:3001 /'时发送index.html 然后我得到'localhost:3001 / test'和Express抛出错误404

1 个答案:

答案 0 :(得分:0)

将此添加到您的Nginx应用配置:

location /{
...
try_files $uri %uri/ /index.html
}