【问题标题】:react router v4 dployment on ec2 return 404在 ec2 上反应路由器 v4 部署返回 404
【发布时间】:2018-01-05 15:18:53
【问题描述】:

我的本​​地 React 和 Node.js 应用程序在本地运行,但所有 React 路由在服务器上都失败了。

在服务器 (aws) 上会显示 index.html,并且我还收到了尝试使用用户和密码进行身份验证的积极反馈(所以我假设 node.js 路由一切正常)。 但所有 React Router 内部路径都是“404”。

主 React 路由页面如下所示:

import { Redirect, BrowserRouter as Router, Route, Switch } from 'react-router-dom'

render() {
    return (
      <Router>
        <Switch>
          <Route exact path='/' render={() => (<Login setRedirect={this.setRedirect} />)} />
          <Route exact path='/edit-menu' component={EditMenu} />
          <Route path='/view-menu/:rest_id' component={ViewMenu} />
        </Switch>
      </Router>
    );
  }

登录成功后页面会跳转到: serverhostname:8081/edit-menu(在本地主机上)

我希望主机名/编辑菜单显示到 EditMenu 的组件,就像它在 localhost 上一样,但在服务器上返回“404”。

Git:https://github.com/Yanipo5/MenuCreator
注:我红https://stanko.github.io/react-router-subfolder-on-server/ 它说它与试图将路由映射到根目录的开发服务器有关????不确定,但目前我的服务器项目结构是 (root

【问题讨论】:

    标签: reactjs react-router


    【解决方案1】:

    添加了 node.js 以捕获并将所有静态调用重定向到 index.html

    // handle every other route with index.html, which will contain // a
    script tag to your application's JavaScript file(s). 
    
    app.get('*', function (request, response){  
    response.sendFile(path.resolve(__dirname, 'public', 'index.html')) })
    

    进一步阅读: https://github.com/ReactTraining/react-router/blob/v2.0.0-rc5/docs/guides/basics/Histories.md#configuring-your-server

    【讨论】:

      猜你喜欢
      • 2018-03-08
      • 2021-01-22
      • 2022-12-01
      • 2017-10-05
      • 2017-10-21
      • 2017-12-06
      • 1970-01-01
      • 2020-09-08
      • 2017-09-16
      相关资源
      最近更新 更多