Node.js provides a default writable stream and readable stream. Process.stdout and Process.stdIn are writable and readable streams respectively.
Let’s understand this with an example.
Standard Input and Standard Output:
App1: Let's create an example with standard input and standard output
const questions = [ "What is your name?", "What would you rather be doing?", "What is your preferred programming language?" ];
const ask = (i = 0) => { process.stdout.write(`\n\n\n ${questions[i]}`); process.stdout.write(` > `); };
ask();
Create an another array to collect the answers to respective questions
const answers = [];
process.stdin.on("data", data => { });
In the callback, we will push the data to answers array.
process.stdin.on("data", data => { answers.push(data.toString().trim()); });
process.stdin.on("data", data => { answers.push(data.toString().trim()); if (answers.length < questions.length) { ask(answers.length); } else { process.exit(); } });
process.on("exit", () => { const [name, activity, lang] = answers; process.stdout.write.log(`Thank you for your anwsers. Go ${activity} ${name} you can write ${lang} code later!!! `); });
ask(0);
varquestions = [ “What is your name?", "What would you rather be doing?", "What is your preferred programming language?" ]; varanswers = []; functionask(i) { process.stdout.write(`\n\n${questions[i]} >`); } process.stdin.on('data', function(data) { answers.push(data.toString().trim()); if (answers.length < questions.length) { ask(answers.length); } else { process.exit(); } }); process.on('exit', function() { process.stdout.write(`\n\n\n${answers[1]}${answers[0]}, ${answers[2]} is awesome!\n\n`); }); ask(0);
Output:
node ask What is your name? > Programmer What is your fav hobby? > Programming What is your preferred programming language? > Javascript Programming Programmer, Javascript is awesome!
Stream interface provides to read and write data. We can use this to communicate with files, the internet, other processes. In fact, we been using this in the above example and let's go through this with another example on file streams.
App1:
console.log(`I read ${data}`); });
The advantage of reading bit by bit is that we need not incur a lot of memory for reading the entire file. Also, we can control because of raise events.
For eg. readStream.once(“data”, … ); to display the very first little bit of data read. To know that reading is ended, we can listen to ‘end’ event.
constfs = require("fs"); constreadStream = fs.createReadStream("./readFile.txt", "UTF-8"); console.log("type something..."); readStream.on("data", data=> { console.log(`I read ${data}`); }); readStream.once("data", data=> { console.log("reading once"); }); readStream.on("end", () => { console.log(`reading completed`); });
Even we can pipe it to another writable stream to update the read stream. To do this, let's understand the writable stream with an example.
App2:
We have already used a writable stream i.e. process.stdout.write(“hello world”); Let's use the fs module to write data to file.
process.stdin.on(“data”, data => { writeStream.write(data); }
App3:
Lets further extend this example to incorporate the readStream to read from a file and update the writeStream.
Normally, any readable stream is designed to work with any writable stream. This helps us in piping us the readable stream with a writable stream.
For example:
// this way what ever we type in terminal is been written to the writeStream i.e. the file.
constfs = require("fs"); constwriteStream = fs.createWriteStream("./myFile.txt", "UTF-8"); constreadStream = fs.createReadStream("./readFile.txt", "UTF-8"); readStream.pipe(writeStream);
JavaScript is a dynamic computer programming language for the web. Jav...
Introduction: Angular (What is Angular?)Angular was formerly introdu...
Leave a Reply
Your email address will not be published. Required fields are marked *