Amit Merchant
Amit Merchant

Reputation: 1043

How to pipe multiline input to the Node.js program?

I've built a Node.js program which basically takes a multiline input using readline module via prompt. Here's the code for the same

    let lineReader = readline.createInterface({
        input: process.stdin,
        output: process.stdout
      });

    lineReader.prompt();  

    let i = 0;
    let communityCards = [];
    let evalCards = [];

    lineReader.on('line', line => {

        console.log('line', line);
    });

    lineReader.on('close', ()=>resolve(evalCards));

Here's my multiline input

KS AD 3H 7C TD
John 9H 7S
Sam AC KH
Becky JD QC

It is working fine because it takes input from prompt upon running node index.js and outputs to stdout line by line but I want this multiline input passed to my program via piping via stdin and output to stdout upon hitting the Enter. Something like below:

$ multiline-input | node index.js

Can someone help me in figuring this out?

Upvotes: 1

Views: 975

Answers (1)

JSchirrmacher
JSchirrmacher

Reputation: 3364

This seems a rather shell related question.

If you qoute your multi-line input, it should work. Something like that:

$ echo 'KS AD 3H 7C TD
John 9H 7S
Sam AC KH
Becky JD QC' | node index.js

EDIT

As I understand your question now, you want to add each line read to the array evalCards, so just use the following handler:

lineReader.on('line', line => {
    evalCards.push(line)
})

Upvotes: 1

Related Questions