我使用webpack dev服務器來運行本地Web服務器。Webapck服務器的路由無法獲取/ page
當我使用瀏覽器輸入網址/localhost:8080/sign_in
,我得到了錯誤。
無法獲取/ sign_in
我在webpack.config.js
添加對象historyApiFallback: true
,並得到解決。
devServer: {
inline: true,
port: 8080,
historyApiFallback: true,
headers:{
'Access-Control-Allow-Origin': '*',
"Access-Control-Allow-Methods": "POST, GET, OPTIONS"
}
}
但是,當我編譯webpack -p
和運行node server.js
再次發生同樣的錯誤。
其次server.js
代碼
const express = require('express');
const path = require('path');
const port = process.env.PORT || 8080;
const app = express();
const history = require('connect-history-api-fallback');
const indexPath = path.join(__dirname, './public/index.html')
const publicPath = express.static(path.join(__dirname, '/public'))
app.use(publicPath)
app.use(history())
app.get('/', (req, res) => {
res.sendFile(path.resolve(indexPath));
});
app.listen(port);
我怎麼固定呢?
你只定義了路徑'/'。你也需要添加'/ sign_in'。 – Seblor
@Seblor我在我的應用程序中使用了react-routes,所以每個頁面都依賴於/index.html –