我的 Heroku 应用程序使用 React 和 React Router。我使用 Switch 浏览不同的组件,因此 URL 也会发生变化(例如/room/4141
)。但是,如果我重新加载页面,它不会像 React 应用程序那样运行,而是会搜索提到的.html
文件。
我使用了这个 Buildpack:https : //github.com/mars/create-react-app-buildpack.git但它似乎对重写为index.html
.
有没有办法防止这种行为并将所有 URL 重写为index.html
?
**编辑:我对快递不够熟悉,但这index.html
是服务的方式。
const express = require("../../node_modules/express");
const app = express();
const server = require("http").Server(app);
const io = module.exports.io = require('../../node_modules/socket.io/lib')(server)
const path = require("path")
app.use(express.static(path.join(__dirname, '../../build')));
if(process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, '../../build')));
console.log("DEBUG HERE", __dirname, path.join(__dirname+'../../build'));
//
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname+'../../build/index.html'));
})
}
//build mode
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname+'../../public/index.html'));
})