2016-04-22 10 views
1

クライアント側にはreact、サーバ側にはexpressを使用してWebアプリケーションを作成しています。
クライアントページをルーティングするreact-routerlink)を使用しています。
hashHistoryで作業するのは簡単で、うまくいきましたが、今はbrowserHistoryを使いたいと思います。サーバルートを使ったリアルータのブラウザ履歴

react-routerチュートリアルで説明したように、私はサーバにクライアントの要求を期待する必要があるので、クライアントページをレンダリングするときにはindex.htmlをサーバにします。
クライアントからのページ要求とサーバー処理要求の両方を処理する方法が見つかりません。

たとえば、私はパス/productにページを持っていますが、サーバー処理用のエンドポイントもあります。/authenticate

// 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')) 
}) 

しかし、この方法はindex.htmlにそれを返信する(/authenticateを含む)すべてのリクエストを発生します:それは次のように使用することを言いますreact-routerチュートリアルで

。どのように私はこれらの2つをマージできますか?

答えて

3

あなたは(基本的にはキャッチオールで、最後に来て持っている)

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')) 
}) 
*/authenticateを定義する必要があります