Aaron Yodaiken
Aaron Yodaiken

Reputation: 19551

build programs that can be piped in node.js

I'd like to make utilities in Node JS which can be used like:

node util.js | node util2.js

just as you would using say

cat * | grep str

etc.

Upvotes: 5

Views: 159

Answers (1)

s4y
s4y

Reputation: 51735

Use the process.stdin and process.stdout streams.

Here's an example from those docs:

process.stdin.resume();
process.stdin.setEncoding('utf8');

process.stdin.on('data', function (chunk) {
  process.stdout.write('data: ' + chunk);
});

process.stdin.on('end', function () {
  process.stdout.write('end');
});

Calling process.stdin.resume() starts the flow of data from standard input and will keep your program running until stdin is paused or ends.

Upvotes: 3

Related Questions