2016-04-22 132 views
1

我正在编写一个Web应用程序,它使用react作为客户端,express作为服务器端。
我正在使用react-routerlink)路由客户端页面。
使用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。我怎样才能合并这两个?

回答

3

你必须定义*/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')) 
}) 
相关问题