Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pipe to function in node.js?

Tags:

node.js

I want to read from file to stream, pipe the output to a function that will upperCase the content and then write to file. This is my attempt. What am I doing wrong?

const fs = require('fs')

const fred = q => {
    return q.toUpperCase()
}

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

Currently the error is:

dest.on is not a function

like image 459
danday74 Avatar asked Mar 08 '18 09:03

danday74


People also ask

How do I pipe data in Node JS?

pipe() Method. The readable. pipe() method in a Readable Stream is used to attach a Writable stream to the readable stream so that it consequently switches into flowing mode and then pushes all the data that it has to the attached Writable.

What is pipe method in Node JS?

Pipes can be used to connect multiple streams together. One of the most common example is to pipe the read and write stream together for the transfer of data from one file to the other. Node. js is often also tagged as an event driven framework, and it's very easy to define events in Node. js.

What is pipe () in JS?

The JavaScript Pipeline Operator ( |> ) is used to pipe the value of an expression into a function. This operator makes chained functions more readable. This function is called using ( |> ) operator and whatever value is used on the pipeline operator is passed as an argument to the function.

What does it mean to pipe a function?

A pipe function takes an n sequence of operations; in which each operation takes an argument; process it; and gives the processed output as an input for the next operation in the sequence. The result of a pipe function is a function that is a bundled up version of the sequence of operations.

What does pipe() method do in Node JS?

What Does .pipe () Method Do? The method .pipe was added in v0.9.4 of Node.js and its purpose is to attach a writeable stream to a readable stream allowing to pass the readable stream data to the writeable stream. One good way to understand this concept is by thinking about PVC pipes and connecting two pipes.

How to define a function in Node JS?

This is exactly similar to functions in JavaScript. The structure of a function is given below: Now, functions in Node.js must return a value. So function definitions include an optional return statement. However, if the return statement is missing, then the function returns undefined. After a function is defined, you can then call it.

What is a pipe in Python?

The concept of pipe is simple — it combines n functions. It’s a pipe flowing left-to-right, calling each function with the output of the last one. Let’s write a function that returns someone’s name.

What is a stream in Node JS?

How to use stream.pipe 2011-08-26 If you've been using Node.js for a while, you've definitely run into streams. HTTP connections are streams, open files are streams; stdin, stdout, and stderr are all streams as well. A 'stream' is node's I/O abstraction - if you feel like you still need to understand them better, you can read more about them here.


3 Answers

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'))
like image 193
danday74 Avatar answered Oct 16 '22 08:10

danday74


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! :)

like image 13
Marco Talento Avatar answered Oct 16 '22 08:10

Marco Talento


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.

like image 4
Steven Lu Avatar answered Oct 16 '22 06:10

Steven Lu