2016-04-22 105 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')) 
})