create-react-appでルートパスをカスタムした時のStatic Serverの起動について
create-react-appの仕様
create-react-appのビルドでは、package.json
のhomepage
の値を参照しています。
無指定だとルートパスは/
とります。package.jsonに"homepage": "https://example.com/custom
と設定するとルートパスは/custom
になります。
Static Serverでの起動
create-react-appでStatic Serverの起動にはserve
が推奨されています。
serve
はルートパスが必ず/
になり、カスタムすることができません。
ルートパスをを変更した場合は、express
などのWebフレームワークを使って、自作する必要があります。
express
を利用した例は以下のとおりです。
const express = require('express');
const path = require('path');
const app = express();
app.use("/custom", express.static(path.join(__dirname, 'build')));
app.get('/custom', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(9000);
これでhttp://localhost:9000/custom
からアクセスできるようになります。