How to pipe to function in node.js?

You have to use Transform if you want to "transform" streams. I recommend you to read: https://community.risingstack.com/the-definitive-guide-to-object-streams-in-node-js/

const fs = require('fs')

const Transform = require('stream').Transform;

  /// Create the transform stream:
  var uppercase = new Transform({
    decodeStrings: false
  });

  uppercase._transform = function(chunk, encoding, done) {
    done(null, chunk.toString().toUpperCase());
  };

fs.createReadStream('input.txt')
.pipe(uppercase)
.pipe(fs.createWriteStream('output.txt'))

EDIT: You need to call .toString() in chunk because it's a buffer! :)


Using async iteration is the cleaner new way to transform streams:

const fs = require('fs');
(async () => {
    const out = fs.createWriteStream('output.txt');
    for await (const chunk of fs.createReadStream('input.txt', 'utf8')) {
        out.write(chunk.toUpperCase());
    }
})();

As you can see, this way is a lot more terse and readable if you already are working in an async function context.


Based on answer from Marco but tidied up:

const fs = require('fs')
const {Transform} = require('stream')

const upperCaseTransform = new Transform({
    transform: (chunk, encoding, done) => {
        const result = chunk.toString().toUpperCase()
        done(null, result)
    }
})

fs.createReadStream('input.txt')
    .pipe(upperCaseTransform)
    .pipe(fs.createWriteStream('output.txt'))

Tags:

Node.Js