You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Express服务React站点时,含参数的多级路径页面刷新/直接访问空白问题排查求助

Express服务React站点时,含参数的多级路径页面刷新/直接访问空白问题排查求助

嘿,我遇到了个挺头疼的问题,想请教下各位大佬。我用Express做后端服务,前端是React项目,靠react-router-dom做路由管理。正常在站点内跳转的时候一切都没问题,但只要刷新带参数的多级路径页面(比如/repairs/repair/123),或者直接在地址栏输入这个路径访问,页面就完全空白——既不显示404,也不加载我写的处理无效参数的页面。

我的后端index.js代码如下:

const express = require('express');
const path = require('path');
const cors = require('cors');

require('dotenv').config();

const app = express();
const frontEndPath = path.join(__dirname, '..', 'frontend', 'build')

app.use(cors());
app.use(express.json());
app.use(express.static(frontEndPath));

app.get('/api', (req, res) => {
    res.send('Hello from the API!')
})

// Various API Routes From Different Files I Have Omitted

app.get('/*', (req, res) => {
    res.sendFile(path.join(frontEndPath, 'index.html'));
});

const PORT = process.env.PORT;
app.listen(PORT, () => {
    console.log('Running on port', PORT);
});

前端的路由配置是这样的:

<div className='main'>
    <Routes>
        <Route path='/' element={<Dashboard />} />
        <Route path='/repairs' element={<Repairs />} />
        <Route path='/repairs/repair/:id' element={<Repair />} />
        <Route path='/calendar' element={<Calendar />} />
        <Route path='/customers' element={<Customers />} />
        <Route path='/settings' element={<Settings />} />
    </Routes>
</div>

我已经尝试调整过后端那个/*的兜底路由,还单独给这个带参数的页面加过路由,但都没解决问题,感觉肯定是哪里漏了,但找了好久都没头绪,有没有大佬能指点一下?

备注:内容来源于stack exchange,提问作者Josh Cox

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.17 12:54:51