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

Node.js服务AJAX调用耗时超40秒,Postman仅需5秒求助排查

问题描述

我有一个Node.js服务,会向后端服务器发起两次AJAX调用。当前遇到的问题是:通过前端AJAX调用该服务时,耗时超过40秒才能完成,但使用Postman直接测试同一后端服务时,响应仅需5秒。

Node.js服务代码
// ...
const PORT = process.env.PORT || 5001;
const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.listen(PORT, () => {
  console.log(`Server listening on ${PORT}`, new Date());
});

app.get('/api', (req, response, next) => {
    console.log(`[${new Date()}] API called...`);
    let user = md5(req.query.user+'mytest').substring(0,5)
    let secretcode = req.query.secretcode
    let type = req.query.type
    console.log(`[${new Date()}] User: ${user}, Secret Code: ${secretcode}, Type: ${type}`);
    if(user == secretcode) {
      console.log(`[${new Date()}] Initiating checkApiUser request...`);
      axios.post('http://192.168.xxx.xxx:xxxx/checkUser', qs.stringify({
        acUsername: req.query.user,
        acPassword: secretcode
      })).then(res => {
        console.log(`[${new Date()}] checkApiUser request completed.`);

        console.log(`[${new Date()}] Initiating apiCatalog request...`);
        axios.post('http://192.168.xxx.xxx:xxxx/catalog', qs.stringify({
          data1: res.data.data1,
          data2: res.data.data2,
          data3: req.query.data3 ? req.query.data3 : 0,
          data4: req.query.data4 && req.query.data4 == "some" || req.query.data4 == "other" ? 0 : 1,
          data5: req.query.data5 ? req.query.data5 : "",
          //...
        })).then(resp => {
          console.log(`[${new Date()}] apiCatalog request completed.`);
          let products = resp.data.product;
          if(type == "xml") {
            response.set('Content-Type', 'text/xml');
            return response.send(o2x({
                '?xml version=\"1.0\" encoding=\"utf-8\"?' : null,
                catalog: {
                    product: products
                }
            }));
          } else {
            return response.json({catalog: products})
          }
        }).catch(err => {
          console.log(err)
        })
      }).catch(err => {
        console.log(err)
      });
    }
});
日志信息
[Wed May 29 2024 14:02:47 GMT+0200 (Central European Summer Time)] API called...
[Wed May 29 2024 14:02:47 GMT+0200 (Central European Summer Time)] User: sample, Secret Code: 34sdg, Type: undefined
[Wed May 29 2024 14:02:47 GMT+0200 (Central European Summer Time)] Initiating checkApiUser request...
[Wed May 29 2024 14:02:47 GMT+0200 (Central European Summer Time)] checkApiUser request completed.
[Wed May 29 2024 14:02:48 GMT+0200 (Central European Summer Time)] Initiating apiCatalog request...
[Wed May 29 2024 14:03:34 GMT+0200 (Central European Summer Time)] apiCatalog request completed.
Postman请求截图

Postman请求截图

问题原因分析及排查建议

从日志可见,checkUser请求瞬间完成,但catalog请求耗时46秒,而Postman直接调用后端catalog接口仅需5秒,说明问题出在Node.js服务转发环节或前端与Node.js的交互上,具体原因和排查方向如下:

可能原因

  • 请求参数不一致:前端通过Node.js转发的catalog请求参数,和Postman直接调用的参数存在差异(比如日志中Type为undefined,可能存在参数缺失/格式错误),导致后端处理延迟。
  • Axios配置差异:Postman的请求头、超时设置、代理等配置,与Node.js中Axios的默认配置不匹配,比如未设置超时、Content-Type不符合后端预期,导致请求阻塞。
  • 跨域预检问题:前端AJAX请求触发OPTIONS预检请求,若Node.js服务未正确处理,会导致预检等待超时,拉长整体耗时。
  • Node.js服务资源瓶颈:服务器CPU/内存不足,或存在事件循环阻塞,导致请求处理排队。
  • 网络路由差异:Node.js服务器到后端服务器的网络路由,与Postman所在机器的路由不同,存在延迟或丢包。

排查建议

  • 比对请求参数:在Node.js中打印catalog请求的完整参数,与Postman调用的参数逐一对比,确保完全一致。
  • 调整Axios配置:给Axios添加明确超时(如timeout: 10000),设置正确的Content-Type头(如headers: {'Content-Type': 'application/x-www-form-urlencoded'}),观察耗时变化。
  • 检查跨域预检:在浏览器开发者工具Network面板中,查看OPTIONS请求的响应状态和耗时,若有问题,用cors中间件正确处理跨域。
  • 监控资源使用:用pm2 monit或top查看Node.js进程的CPU/内存占用,用clinic bubbleprof分析事件循环是否阻塞。
  • 测试网络连通性:在Node.js服务器上用curl直接调用后端catalog接口,测试耗时是否与Postman一致,若不一致排查服务器防火墙、路由规则。
  • 完善错误捕获:将catch块的console.log(err)改为打印err.response、err.request等详细信息,排查隐藏的请求错误。

内容的提问来源于stack exchange,提问作者Ivan Z.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 19:32:32