Javascript 对路由器浏览器历史记录与服务器路由作出反应
我正在编写一个web应用程序,客户端使用Javascript 对路由器浏览器历史记录与服务器路由作出反应,javascript,express,reactjs,react-router,Javascript,Express,Reactjs,React Router,我正在编写一个web应用程序,客户端使用react,服务器端使用express。 我正在使用react router()路由客户端页面。 使用hashHistory很简单,效果很好,但现在我想使用browserHistory 正如react router教程中提到的,我需要告诉我的服务器期望客户机请求,因此当我们呈现客户机页面时,它将为index.html 我找不到一种方法来处理来自客户端的页面请求和服务器处理请求 例如,我在路径/product中有一个页面,但我也有一个用于服务器处理/auth
react
,服务器端使用express
。我正在使用
react router
()路由客户端页面。使用
hashHistory
很简单,效果很好,但现在我想使用browserHistory
正如react router
教程中提到的,我需要告诉我的服务器期望客户机请求,因此当我们呈现客户机页面时,它将为index.html
我找不到一种方法来处理来自客户端的页面请求和服务器处理请求 例如,我在路径
/product
中有一个页面,但我也有一个用于服务器处理/authenticate
的端点
在react router
教程中,它说要使用以下内容:
// server.js
// ...
// add path.join here
app.use(express.static(path.join(__dirname, 'public')))
// ...
app.get('*', function (req, res) {
// and drop 'public' in the middle of here
res.sendFile(path.join(__dirname, 'public', 'index.html'))
})
但这种方式将导致每个请求(包括
/authenticate
)将其发送回index.html
。如何合并这两者?您必须在*
之前定义/authenticate
(这基本上是一个包罗万象的问题,必须排在最后)
app.use(express.static(path.join(__dirname, 'public')))
ap.get('/authenticate', function (req, res) {
res.sendStatus(200)
});
// ...
app.get('*', function (req, res) {
// and drop 'public' in the middle of here
res.sendFile(path.join(__dirname, 'public', 'index.html'))
})