Interfacing with standard I/O
Standard I/O relates to the predefined input, output, and error channels that connect a process to a shell terminal, commonly known as STDIN, STDOUT, and STDERR. Of course, these can be redirected and piped to other programs for further processing, storage, and so on.
Node provides access to standard I/O on the global process
object.
In this recipe, we're going to take some input, and use it to form some data which we'll send to STDOUT, while simultaneously logging to STDERR.
Getting ready
Let's create a file called base64.js
. We're going to write a tiny program that converts input into its Base64
equivalent.
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...