我有我想要 React Router 处理的路径,而且我还有一个 Express API 后端,我从 React 应用程序调用它来执行一些安全的 API 调用。
/#/
- 希望在这里提供应用程序
/#/:id
- 应用程序的唯一 URL,我使用 ID 从 React 调用某些快速端点。
*
- 所有快速 REST 端点
Express app.js:
app.use(middleware);
app.use(require('webpack-hot-middleware')(compiler, {
log: console.log
}));
app.use('/api', [router_invokeBhApi]);
app.get('/#/*', function response(req, res) {
res.write(middleware.fileSystem.readFileSync(path.join(__dirname, 'dist/index.html')));
res.end();
});
app.use(express.static(path.join(__dirname, '/dist')))
我的 React 路由器组件:
export default (
<Route path="/" component={App}>
<IndexRoute component={HomePage} />
<Route path="/:id" component={ConsentForm} something="dope"/>
</Route>
);
所以这就是发生的事情:
- 即将localhost:8000
为应用程序提供服务HomePage
成分
- 即将localhost:8000/#/
还为该应用程序提供服务HomePage
成分
- 即将localhost:8000/example
给我Cannot GET /example
这意味着快递正在运作
- 即将localhost:8000/api
给了我一个从express发送的测试JSON对象,这是正确的。
- 即将localhost:8000/#/somehashcode
仍然给我HomePage
组件,当它应该给我ConsentForm
成分。
我使用 React Dev 工具检查了 Router 组件:
- RouterContext 组件有一个名为的对象routes
with a childRoutes
在 - 的里面routes[0]
,并且它有路径/:id
. routes[0]
也有一条路/
这告诉我 React Router 正确加载了所有路由?
如此迷茫...
我在其中渲染整个应用程序的 React 文件:
import 'babel-polyfill';
import React from 'react';
import ReactDOM from 'react-dom';
import { Router, browserHistory } from 'react-router';
import Routes from './shared/components/Routes';
import './shared/base.css';
import './shared/customInput.css';
const ROOT_ELEMENT = 'app';
ReactDOM.render((
<Router history={browserHistory}>
{Routes}
</Router>
), document.getElementById(ROOT_ELEMENT));