How to do it...

First, we'll listen for a data event on process.stdin:

process.stdin.on('data', (data) => { 
// we can leave this empty for now
})

If we run our file now:

$ node base64.js 

We should notice that the process does not exit, and allows keyboard input.

Now let's do something with the incoming data. We'll modify our code in base64.js  like so:

process.stdin.on('data', data => { 
process.stderr.write(`Converting: "${data}" to base64\n`)
process.stdout.write(data.toString('base64') + '\n')
})

We can test our program like so:

$ echo -e "hi\nthere" | node base64.js 

The above should output:

Converting: "hi 
there
" to base64
aGkKdGhlcmUK

We can also simply run our program, and each line of input will be converted:

$ node base64.js 
<keyboard input>

Of course, if we want to filter out logs to STDERR (STDERR means standard error output), we can do the following:

$ echo -e "hi\nthere" | node base64.js 2> /dev/null 

The above outputs:

aGkKdGhlcmUK