How to Pipe Data Into a NodeJS Script?

pipenodejsjavascriptterminallinuxscript

How to pipe data into a NodeJS script code.

Once upon a time in your coding life, you wanted to send data from the terminal to your script. My main languages are Typescript and JavaScript, then usually fix problems with these two.

I will propose an example of how to read a file with cat and then pipe the result of the cat to a NodeJS script.

TLDR;

itsbetma/how-to-pipe-into-nodejs (github.com)

> cat data.txt | node pipe.js// pipe.js
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (data) {
console.log(data);
});

What is CAT?

The cat command line tool is a Unix/Linux utility that is used to concatenate files and print them to standard output. It can also be used to create new files and edit existing ones.

The cat command line tool is a Unix/Linux utility that is used to concatenate files and print them to standard output. It can also be used to create new files and edit existing ones. However, there are several other command line tools that are similar to cat and can be useful for manipulating and displaying text files in various ways. Here are some of them:

  • head: prints the first few lines of a file. This can be useful when you only need to see the beginning of a file and don't want to process the entire file.
  • tail: prints the last few lines of a file. This can be useful when you need to see the end of a file, such as when monitoring log files.
  • echo: prints a message or variable to the console. This is useful when you need to display a message or variable value in the console.

How to pipe data into a Node.js script

Piping data into a Node.js script can be a useful technique for passing data into a script from an external source. One way to pipe data into a Node.js script is to use the command line interface. To do this, first navigate to the directory where the script is located. Then, use the cat command to read the contents of the file containing the data you want to pipe into the script. Finally, use the pipe operator | to pass the output of the cat command as input into the Node.js script. For example, if your data is stored in a file called data.txt and your Node.js script is called pipe.js, you could pipe the data into the script using the following command:

cat data.txt | node pipe.js

In the pipe.js file, you can use the process.stdin object to read the piped data. For example, the following code reads the piped data and logs it to the console:

process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (data) {
console.log(data);
});

process.stdin is used in a Node.js script to read the data that is piped into it through the command line. The script reads the piped data using the process.stdin.on method, which listens for data events occurring in the input stream. When the script receives data, it logs it to the console using the console.log method. This allows the script to process the data as necessary using JavaScript code.

Useful case: hashing a file

Let’s assume that we have a file with a text inside called data.txt what we want to accomplish is to hash using AES encryption the content of the file.

To handle all the hard work of the encryption we need the Crypto-JS package and a small change on the script:

// pipe.js
const CryptoJS = require('crypto-js')
function encrypt(data) {
const secretKey = CryptoJS.enc.Utf8.parse('YOUR_SECRET_KEY') // Put it on the .env file
const ciphertext = CryptoJS.AES.encrypt(data, secretKey, { iv: secretKey }).toString() // Encryption
return ciphertext
}
process.stdin.resume()
process.stdin.setEncoding('utf8')
process.stdin.on('data', function (data) {
console.log('data from pipe: ', data)
console.log('encrypted data:', encrypt(String(data)))
})

How to use it?

> cat data.txt | node pipe.js
---
data from pipe: thisismypassword
encrypted data: vxDXJ0lqDdlT894inWBQjspSq3U2X4hC583kGUysIGU=

or

> echo "thisismypassword" | node pipe.js
----
data from pipe: thisismypassword
encrypted data: vxDXJ0lqDdlT894inWBQjspSq3U2X4hC583kGUysIGU=

This is the end…

Now use your imagination to pipe any type of data into NodeJS scripts. Let’s code.

And if you like content about Git, Linux, Productivity tips, and Typescript please follow me on Medium Marco Antonio Bet and checkout the itsbetma.com.

See you soon.

Git repository:

Share?