简体   繁体   English

如何在Node.js中“累积”原始流?

[英]How can I 'accumulate' a raw stream in Node.js?

At the moment I concatenate everything into a string as follows 目前我将所有内容连接成一个字符串,如下所示

var body = '';
res.on('data', function(chunk){
    body += chunk;
});

How can I preserve and accumulate the raw stream so I can pass raw bytes to functions that are expecting bytes and not String? 如何保存和累积原始流,以便将原始字节传递给期望字节而不是字符串的函数?

Better use Buffer.concat - much simpler. 更好地使用Buffer.concat - 更简单。 Available in Node v0.8+. 可在Node v0.8 +中使用。

var chunks = [];
res.on('data', function(chunk) { chunks.push(chunk); });
res.on('end', function() {
    var body = Buffer.concat(chunks);
    // Do work with body.
});

First off, check that these functions actually need the bytes all in one go. 首先,检查这些函数是否真的需要一次性完成所有字节。 They really should accept 'data' events so that you can just pass on the buffers in the order you receive them. 他们真的应该接受'data'事件,这样你就可以按照收到它们的顺序传递缓冲区。

Anyway, here's a bruteforce way to concatenate all data chunk buffers without decoding them: 无论如何,这是一种连接所有数据块缓冲区而不解码它们的强力方法:

var bodyparts = [];
var bodylength = 0;
res.on('data', function(chunk){
    bodyparts.push(chunk);
    bodylength += chunk.length;
});
res.on('end', function(){
    var body = new Buffer(bodylength);
    var bodyPos=0;
    for (var i=0; i < bodyparts.length; i++) {
        bodyparts[i].copy(body, bodyPos, 0, bodyparts[i].length);
        bodyPos += bodyparts[i].length;
    }
    doStuffWith(body); // yay
});

Alternately, you can also use a node.js library like bl or concat-stream: 或者,您也可以使用像bl或concat-stream这样的node.js库:

'use strict'
let http = require('http')
let bl = require('bl')
http.get(url, function (response) {
  response.pipe(bl(function (err, data) {
    if (err)
      return console.error(err)
    console.log(data)
  }))
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何向node.js / express&#39;发送或获取其原始输出流提供输入流? - How can I provide an input stream to node.js/express' send, or get its raw output stream? 如何使用node.js启动/停止请求流? - How can I start / stop a request stream with node.js? 如何从可写流中创建可读流? (Node.js的) - How can I make a Readable stream from a Writable stream? (Node.js) 如何通过 Node.JS 传输 output 原始图像? - how can I transfer output raw images through Node.JS? 如何使用 Node.js 中的请求库获取原始 HTTP 消息正文? - How can I get the raw HTTP message body using the request library in Node.js? 如何将Highland.js或Node.js流限制为每秒一个对象? - How can I throttle a Highland.js or Node.js stream to one object per second? 如何创建一个Node.js代理可读流,它将包装另一个在创建代理流时不可用的流? - How can I create a Node.js surrogate readable stream that will wrap another stream that's not available at the time the surrogate stream was created? 在使用node.js的lambda中,我如何解析kinesis流中的数据 - In lambda using node.js how can I parse the data from a kinesis stream 如何使用Node.js和Websockets创建Twitter流? - How can I create a Twitter stream using Node.js and Websockets? 如何从node.js中的连续图像流创建MediaStream轨道? (用于WebRTC) - How can I create a MediaStream track from a continuous stream of images in node.js? (for usage with WebRTC)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM