1

无论我做什么,当路径为“/”时它都会起作用,但没有别的。

const App = () => (
  <Router>
    <div>
      <Route exact path="/" component={Home}/>
      <Route path="/test" render={({match}) => match && <h1>Yo</h1>} />
    </div>
  </Router>
)

当我创建路径“/”时,任何一个都可以工作,但我无法获得任何其他工作路线,并且我收到消息“无法获取 /test”。我认为该错误可能与我的服务器或 webpack 有关,因为我可以在使用 Create-react-app 时使路由正常工作,但我很无能。

我怀疑我的服务器端代码可能做错了什么

const express = require('express');
const app = express();

app.use(express.static('client'));

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

我的目录结构是

client
   compiled
      bundle.js
   App.js
  index.html
  index.js
  results.jsx

server
  index.js

我真的很感激任何帮助。

对于将来遇到此问题的任何人,我都可以通过将以下代码添加到我的服务器文件中来解决这个问题。

app.get('*', (req, res) => {
  res.sendFile(path.resolve(__dirname, '..', 'client', 'index.html'));
});
4

0 回答 0