简体   繁体   中英

Streaming in Node.JS

I would like to know the good practice if I'm streaming data and want to have access to whole data after streaming;

I'm streaming like this:

    res._oldWrite = res.write;
    res.write = function (chunk, encoding, cb) {
        var decoded = chunk.toString(encoding);
        write.write(new Buffer(decoded, encoding), encoding, cb);
        return res._oldWrite.call(res, new Buffer(decoded, encoding), encoding, cb);
    }

Now that I want to access to my data I did something like:

    res._oldWrite = res.write;
    var jsonData = '';
    res.write = function (chunk, encoding, cb) {
        var decoded = chunk.toString(encoding);
        jsonData += decoded;
        write.write(new Buffer(decoded, encoding), encoding, cb);
        return res._oldWrite.call(res, new Buffer(decoded, encoding), encoding, cb);
    }

    res.on('finish', function(){
        // Now I can have access to jsopnData but it is gross ; what is the right way?
    })

But isn't there any better way to do it?

So I'm not 100% sure I understand your question @Web Developer, but since you asked for code, below is all that I meant.

Note that there are probably other shorter ways of doing the same thing (but I'm not sure what you mean by "access whole data after streaming" --- store in memory? all at once? etc):

var dataStream = require('stream').Writable();
//I'm assuming the "real processing" is saving to a file
var fileStream = fs.createWriteStream('data.txt');
var masterStream = require('stream').Writeable();

masterStream._write = function (chunk, enc, next) {

  dataStream.write(chunk);
  fileStream.write(chunk);
  next();
};

//if you now write to master stream, you get values in both dataStream and fileStream
//you can now listen to dataStream and "have access to the data"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM