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) {
        chunks += chunk;
      res.on("end", function () {
        const object = JSON.parse(chunks)
        console.log(Buffer.byteLength(chunks, 'utf8') / 1024 + " kbytes");
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
  • dongmubi4375 2018-02-05 11:23

    Let me guess, you are using body-parser too right? That is causing an issue.

    Body Parser Allows data up to certain limit (100kB I guess) by default.

    To increase the limits go to: node_modules>body-parser>lib>types>json.js

    look for the following line:

    var limit = typeof opts.limit !== 'number'
    ? bytes.parse(opts.limit || '100kb')
    : opts.limit

    replace the 100 in 100kB with the limit you want to impose in your application. This should fix it.


    If you don't want to change node_modules: set the limit option when you declare the bodyparser variable.

    var bodyParser = require('body-parser');
    bodyParser.json({limit: *your limit in bytes*});

    This works only in case of JSON parsing. you can set limit parameter to raw, text, urlencoded formats too similarly (check documentation link below).

    See this.

    Thank You @iofjuupasli for suggesting that there could be a better way.




  • ¥15 VB.NET 父窗体调取子窗体报错
  • ¥15 python海龟作图如何改代码使其最后画出来的是一个镜像翻转的图形
  • ¥15 我不明白为什么c#微软的官方api浏览器为什么不支持函数说明的检索,有支持检索函数说明的工具吗?
  • ¥15 ORBSLAM2框架跑ICL-NUIM数据集
  • ¥15 在我想检测ros是否成功安装时输入roscore出现以下
  • ¥30 老板让我做一个公司的投屏,实时显示日期,时间,安全生产的持续天数,完全没头绪啊
  • ¥15 Google Chrome 所有页面崩溃,三种解决方案都没有解决,我崩溃了
  • ¥20 使用uni-app发起网络请求,获取重定向302返回的cookie
  • ¥20 手机外部浏览器拉起微信小程序支付 (相关搜索:微信小程序)
  • ¥20 怎样通过一个网址找到其他同样模版的网址