top
upGrad KnowledgeHut SkillFest Sale!

Search

Node JS Tutorial

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 Let’s create a file name stdInOut.js and an array with a list of questions. const questions = [    "What is your name?",    "What would you rather be doing?",    "What is your preferred programming language?"  ]; Create an arrow function which takes an index and writes the question out const ask = (i = 0) => {    process.stdout.write(`\n\n\n ${questions[i]}`);    process.stdout.write(` > `);  }; Lets call ask method ask(); Create an another array to collect the answers to respective questions const answers = []; We can listen to input by subscribing to ‘data’ event on standard input. All events are subscribed by calling ‘on’ method with first argument as event name and second argument as callback. 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());  }); Lets keep asking one question after every question till all are answered. And once done, we exit. process.stdin.on("data", data => {  answers.push(data.toString().trim());  if (answers.length < questions.length) {  ask(answers.length);  } else {  process.exit();  }  }); Lets add one more event handler on exit to acknowledge that all answers are captured. 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!!! `);  }); Lets start by default by asking first question. ask(0); Run node stdInOut First question should be displayed. And on answering, next question should be displayed. It should continue till all questions are answered. Post answering all questions, the terminal should display the answers submitted. 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:  Create a readStream.js  and import fs const fs = require(“fs”); lets use readStream to read the file chunk by chunk instead of reading the entire file at once. const readStream = fs.createReadStream(“./readFile.txt”,”UTF-8”); To capture this reading, lets listen to the readStream.on data event. readStream.on(“data”, data => { console.log(`I read ${data}`);  }); run node readStream.js Output should display the content of file chunk by chunk. 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. Create a file writeStream.js const fs = require(“fs”); const writeStream = fs.createWriteStream(“./myFile.txt”,”UTF-8”); writeStream.write(“hello world”); run node writeStream Output has nothing in terminal as the data is written to file. So open the file and see that hello world exists. We can extend this, to read from the terminal and write back to the file system. process.stdin.on(“data”, data => {     writeStream.write(data);  } run node writeStream now the terminal is waiting for us to type something. Enter some text and observe that the content is written back to file. App3:  Lets further extend this example to incorporate the readStream to read from a file and update the writeStream. const readStream = fs.createReadStream(“./readFile.txt”,”UTF-8”); replace the process.stdin with the readStream readStream.on(“data”, …); run node writeStream Observe that the content of the readFile.txt is been read chunk by chunk and written to myFile.txt. 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:  process.stdin.pipe(writeStream); // this way what ever we type in terminal is been written to the writeStream i.e. the file. We can directly use the readStream i.e. readStream.pipe(writeStream); constfs = require("fs");  constwriteStream = fs.createWriteStream("./myFile.txt", "UTF-8");  constreadStream = fs.createReadStream("./readFile.txt", "UTF-8");  readStream.pipe(writeStream);
logo

Node JS Tutorial

Handling Data

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 

  • Let’s create a file name stdInOut.js and an array with a list of questions. 
const questions = [ 
  "What is your name?", 
  "What would you rather be doing?", 
  "What is your preferred programming language?" 
]; 
  • Create an arrow function which takes an index and writes the question out 
const ask = (i = 0) => { 
  process.stdout.write(`\n\n\n ${questions[i]}`); 
  process.stdout.write(` > `); 
}; 
  • Lets call ask method 
ask(); 
  • Create an another array to collect the answers to respective questions 

const answers = []; 

  • We can listen to input by subscribing to ‘data’ event on standard input. All events are subscribed by calling ‘on’ method with first argument as event name and second argument as callback. 
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()); 
}); 
  • Lets keep asking one question after every question till all are answered. And once done, we exit. 
process.stdin.on("data", data => { 
answers.push(data.toString().trim()); 
if (answers.length < questions.length) { 
ask(answers.length); 
} else { 
process.exit(); 
} 
}); 
  • Lets add one more event handler on exit to acknowledge that all answers are captured. 
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!!! `); 
}); 
  • Lets start by default by asking first question. 

ask(0); 

  • Run node stdInOut 
  • First question should be displayed. And on answering, next question should be displayed. It should continue till all questions are answered. 
  • Post answering all questions, the terminal should display the answers submitted. 
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:  

  • Create a readStream.js  and import fs 
  • const fs = require(“fs”); 
  • lets use readStream to read the file chunk by chunk instead of reading the entire file at once. 
  • const readStream = fs.createReadStream(“./readFile.txt”,”UTF-8”); 
  • To capture this reading, lets listen to the readStream.on data event. 
  • readStream.on(“data”, data => { 
console.log(`I read ${data}`); 
}); 
  • run node readStream.js 
  • Output should display the content of file chunk by chunk. 

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. 

  • Create a file writeStream.js 
  • const fs = require(“fs”); 
  • const writeStream = fs.createWriteStream(“./myFile.txt”,”UTF-8”); 
  • writeStream.write(“hello world”); 
  • run node writeStream 
  • Output has nothing in terminal as the data is written to file. So open the file and see that hello world exists. 
  • We can extend this, to read from the terminal and write back to the file system. 
process.stdin.on(“data”, data => { 
   writeStream.write(data); 
} 
  • run node writeStream 
  • now the terminal is waiting for us to type something. 
  • Enter some text and observe that the content is written back to file. 

App3:  

Lets further extend this example to incorporate the readStream to read from a file and update the writeStream. 

  • const readStream = fs.createReadStream(“./readFile.txt”,”UTF-8”); 
  • replace the process.stdin with the readStream 
  • readStream.on(“data”, …); 
  • run node writeStream 
  • Observe that the content of the readFile.txt is been read chunk by chunk and written to myFile.txt. 

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:  

  • process.stdin.pipe(writeStream); 
// this way what ever we type in terminal is been written to the writeStream i.e. the file. 
  • We can directly use the readStream i.e. 
  • readStream.pipe(writeStream); 
constfs = require("fs"); 
constwriteStream = fs.createWriteStream("./myFile.txt", "UTF-8"); 
constreadStream = fs.createReadStream("./readFile.txt", "UTF-8"); 
readStream.pipe(writeStream); 

Leave a Reply

Your email address will not be published. Required fields are marked *

Suggested Tutorials

JavaScript Tutorial

JavaScript is a dynamic computer programming language for the web. JavaScript was first known as LiveScript. Later on, Netscape changed its name to JavaScript because of its popularity and the excitement generated by it. JavaScript is lightweight and most commonly used as a part of web pages supported by most web browsers like Chrome, Internet Explorer, Opera, Safari, Edge, and Firefox.
JavaScript Tutorial

JavaScript is a dynamic computer programming language for the web. Jav...

Read More

Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introduced by Google corporation in 2012 and was considered to be one of the most promising among JavaScript frameworks. It was written completely in JavaScript to separate an application’s logic from DOM manipulation, aiming at dynamic page updates. Angular introduced many powerful features enabling the developer to effortlessly create rich and single-page applications.Topics CoveredThis Angular tutorial will span over eight modules, each module covering numerous individual aspects that you need to gain complete information about Angular. This set of modules serves as an Angular tutorial for beginners along with experienced IT professionals.Here are the topics that will be covered in the Angular tutorial:Get started with Angular.Learn the basics of Angular.Know what Angular Directives.Get an idea of Component Inputs and Outputs of Angular.Know about Forms in Angular.About Services in Angular.Pipes in Angular.HTTP, Routing and Building in Angular.Who can benefit from this tutorial?This Angular tutorial will be helpful to IT professionals such as:Software Developers, Web Application Programmers and IT Professionals Software Architects and Testing Professionals Career aspirants in web development
Angular JS Tutorial

Introduction: Angular  (What is Angular?)Angular was formerly introdu...

Read More

USEFUL LINKS