Saturday 29 April 2017

Trouble with a basic node.js transform stream

I'm learning nodejs right now and am trying to set up a simple transform stream. The code I am creating is similar to a few tutorials - but I keep getting errors. Right now I am just trying to pass data directly though with no changes (I've removed some commented out lines for brevity):

newstream.js:

var fs = require('fs');
var makeBig = require('./makeBig');

var myReadStream = fs.createReadStream(__dirname + '/Sample.txt');
var myWriteStream = fs.createWriteStream(__dirname + '/WriteMe.txt')

myReadStream
.pipe(makeBig)
.pipe(myWriteStream);

makeBig.js:

const Transform = require('stream').Transform;
const makeBig = new Transform({
        transform(chunk, encoding, callback){
        //chunk = chunk.toUpperCase();
        //console.log(chunk);
        this.push(chunk);
        callback();
    }
});

makeBig.js follows the 'Simplified Constructor Approach' form described in NodeJS v7.9.0 documentation.

Here are the errors that I get:

$ node newStream.js 
_stream_readable.js:501
  dest.on('unpipe', onunpipe);
       ^

TypeError: dest.on is not a function
    at ReadStream.Readable.pipe (_stream_readable.js:501:8)
    at Object.<anonymous> (/home/thodges/Workspace/Node/streams/newStream.js:15:2)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:420:7)
    at startup (bootstrap_node.js:139:9)



via Thomas Hodges

No comments:

Post a Comment