dshm8998473 2018-02-05 11:16
浏览 468
已采纳

node.js http响应数据在数据大小很大时终止

I am using nodejs and sending http post request to server. On server side I am running php. My server is returning correct data around 9KB but data in nodejs client is terminated. It works fine if data is less than 6KB. Following is my code

        var reqPost = https.request(optionspost, function(res) {
        res.on('data', function(d) {
            Console.log('Cloud Resp:', d);
            var jsonObj = JSON.parse(d);
        });
    }); 

My print Console.log('Cloud Resp:', d) prints data up to 8KB only. Can someone please help me to understand if this limit is imposed by nodejs or something else and how can I increase the limit

  • 写回答

2条回答 默认 最新

  • dongzhao4036 2018-02-05 12:25
    关注

    I think your data is chunked during transfering PHP server ---> Node server

    • I assume you are using native https module to request from Node side (Correct me if I am wrong)

    So in data event you need to concat the chunk. But you parse it only in end event. If you parse in data event it will show you error for JSON.parse() because of incomplete data

    Here is the sample code, it works with a 500kb data as I tested. Basically native node does not has data limitation in code level.

    var http = require("https");
    
    var options = {
      "method": "GET",
      "hostname": "c16db448-d912-4ce8-823a-db6c51e09878.mock.pstmn.io"
    };
    
    var req = http.request(options, function (res) {
      var chunks = '';
    
      res.on("data", function (chunk) {
        console.log(chunk.length)
        chunks += chunk;
      });
    
      res.on("end", function () {
        const object = JSON.parse(chunks)
        console.log(object.length)
        console.log(Buffer.byteLength(chunks, 'utf8') / 1024 + " kbytes");
      });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料