【问题标题】:Buffer.prototype.toString doesn't print more than 500 charactersBuffer.prototype.toString 不打印超过 500 个字符
【发布时间】:2015-09-06 10:55:45
【问题描述】:

一个 http 请求返回一个不完整的字符串。

https.get(url, function(res) {
 res.on('data', function(data) {
   translationData = data.toString();
    resolve(translationData);
    })
  });

我不能超过 500 个字符。

我想我的代码是模糊的,但是什么会导致这个问题?

我尝试了很多方法,但都失败了。

我在 How to display long messages in logcat 中有类似的东西,但在 nodeJS 中没有可比性。

【问题讨论】:

    标签: node.js buffer


    【解决方案1】:

    你从 http.get 得到的响应对象是一个Stream。 每当接收到 数据时,都会调用“数据”事件处理程序。您需要处理所有“数据”事件并收集其有效负载,直到获得“结束”事件才能获得整个响应。

    一个简单的方法是使用concat-stream module

    var concat = require('concat-stream');
    https.get(url, function(res) {
        res.pipe(concat(function(data) {
           // data is the entire response
        }));
    }
    

    要了解有关流的更多信息,请阅读 substack 的 stream handbook

    【讨论】:

    • 它有效。学习node的时候没注意这个主题。我会看书。谢谢你的好意。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 2013-12-01
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多