服务对静态服务器做出反应,而不会让人遇到404

问题描述 投票:0回答:1

我有一个反应网站服务:https://aero.mysite.com/profile,我在我的反应应用程序中使用路由器和history.push / replace,使得有可能有​​路径名,如https://aero.mysite.com/profile/path?query=number。但是,如果有人将此网址复制并粘贴给其他人,他或她会得到404,因为https://aero.mysite.com/profile/path实际上并不存在于静态服务器上...(我正在使用koa +文件服务我制作的中间件)。这项挑战有哪些解决方案?

node.js reactjs koa pathname
1个回答
1
投票

如果您正在使用BrowserRouter或类似的东西,请将其替换为在Web服务器路径和前端路由之间添加“#”的哈希路由器

浏览器路由器看起来像这个http://example.com/about

哈希路由器看起来像这个http://example.com/#/about

这将阻止Web服务器处理UI路由

欲了解更多信息,请阅读this article

import { HashRouter as Router, Route } from 'react-router-dom';

import App from './components/App';
import Home from './components/Home';
import About from './components/About';
import Services from './components/Services';

render((
  <Router>
    <div>
      <Route exact path="/" component={Home} />
      <Route path="/about" component={About} />
      <Route path="/courses" component={Services} />
    </div>
  </Router>
), document.getElementById('root'));
© www.soinside.com 2019 - 2024. All rights reserved.