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

NodeJS生产环境API请求返回index.html而非数据问题求助

生产环境API请求返回index.html而非预期数据问题

我使用NodeJS作为后端、ReactJS作为前端开发了一款应用,测试阶段运行完全正常,但部署到生产服务器后,发起API请求时,接口返回的是index.html内容,而非预期的接口数据。

后端NodeJS核心代码片段

// 部分导入语句省略

app.use(
  bodyParser.urlencoded({
    extended: true,
    limit: "100mb",
    parameterLimit: 1000000,
  })
);
const port = 5000;

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cors());

app.use(express.static(path.join(__dirname, 'build')));

app.set("trust proxy", true);

// 其他业务代码省略...

app.post('/api/test', async (req, res) => {
  res.send("Hello world!")
});

NodeJS返回结果示例

NodeJS返回index.html的响应示例

前端请求函数示例

前端发起请求的函数示例

前端请求处理代码片段

// 以下是验证token有效性的请求处理逻辑

// 主请求
await api.post(EndPoint, BodyObject, { headers: headers })
    .then(async function (response) {

        // 主请求的响应有两种可能
        // -- 请求返回的业务数据
        // -- /api/auth接口返回的token无效提示

        // 请求失败场景
        if (response.data == "Access Token expired" || response.data == "There's something wrong with token") {
            // 标记token无效,后续调用/newToken接口
            tokenValidity = false;
        } else if (response.data == "Invalid Signature") {
            // 重定向到登录页
            window.location.href = "https://www.link.pt/login";
        }
        // 请求成功场景
        else {
            serverResponse = response.data;
        }
    })

后端server.js中的接口示例

后端server.js中的接口定义示例

内容的提问来源于stack exchange,提问作者Fury

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 04:00:57