目前,我通过在express-server中提供文件,以静态的方式提供react文件:
app.use(express.static(path.join(__dirname, "..", "..", "build")));
app.get("/", function (req, res) {
res.sendFile(path.join(__dirname, "..", "..", "build", "index.html"));
});
app.use(cors());
app.use(bodyParser.urlencoded({ limit: "10mb", extended: false }));
app.use(bodyParser.json({ limit: "10mb", extended: false }));这工作得很好,我可以访问我的页面,发送请求等。
现在我想实现React-Routing,因为当我返回一次时,完全离开我的站点是乏味的。尽管到目前为止我真的很努力,但我还是找不到解决方案,因为我一访问localhost:3000/home就会收到错误,无法使用GET,因为它会立即引用我的express服务器。
有没有办法避免这个问题?
发布于 2021-01-06 07:25:11
您也可以向index.html发送任何请求
这应该低于您已经定义的快速路由。
router.get(['/', '/*'], function(req, res, next) {
res.sendFile(path.join(__dirname, "..", "..", "build", "index.html"));
});如果你想转发任何在express服务器上找不到的东西,你可以这样做
app.use(function(req, res, next) {
res.sendFile(path.join(__dirname, "..", "..", "build", "index.html"));
});https://stackoverflow.com/questions/65587868
复制相似问题