0

我的 webpack 配置使用 HTMLWebpackPlugin 动态生成 index.html 服务。

plugins: [
  new HTMLWebpackPlugin({
    title: 'My App',
    favicon: path.join(__dirname, "../src/public/fav.ico")
  })
]

我像这样公开这个生成的 index.html:

app.use('/', express.static(__dirname));

我正在使用客户端反应路由器,这是我的路线:

export default (
  <Route path="/" component={App}>
    <Route path="home" component={HomePage} />
  </Route>
)

访问http://localhost:1337/工作正常并正确显示我的“应用程序”组件,但是当我尝试访问http://localhost:1337/home时,我得到一个 404,因为它看起来是 SERVER-SIDE 路由 /home .

当然,如果我添加我的 server.js:

app.get('/home', function(req, res) {
  res.json({name: 'john'});
})

调用http://localhost:1337/home返回正确的 json 对象。

我错过了什么?

4

1 回答 1

-1

我通常做的基本路线大致是这样的:

let React = require('react');

let Router = require('react-router');
let DefaultRoute = Router.DefaultRoute();
let Route = Router.Route();


<Route name="app" path="/" handler={require("./components/App"}>
    <DefaultRoute handler={require("./components/HomePage"} />
    <Route name="anotherSite" handler={require("./components/anotherSite"} />
    <Route name="about-us" handler={require("./components/About"} />
</Route>

在命名路线时,您不需要使用特定的路径。作为惯例,路由名称等于路径,除非您有意创建具有不同于名称的路径的路径选项。

如果这没有帮助,请在您的问题中添加更多信息。

于 2016-07-18T18:46:09.417 回答