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返回结果示例

前端请求函数示例

前端请求处理代码片段
// 以下是验证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中的接口示例

内容的提问来源于stack exchange,提问作者Fury
相关产品推荐
相关产品推荐

