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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀