top
upGrad KnowledgeHut SkillFest Sale!

Search

Node JS Tutorial

The node package manager (npm) gives us access to the largest source of open source library. In this course, we are going to understand the core concepts of node.js i.e. event-driven, asynchronous, non-blocking I/O model. Also going to understand how event loop works and applications can handle many connections. We will focus on streams, the file system, the HTTP module. Before that, let's understand the core concept of node.js.Understanding Node.jsLet us understand Node.js working model by considering two restaurants.The first restaurant is a big, nice, fancy restaurant. In this restaurant, every new guest represents a new user, and making an order is like making a request. If I place an order for a salad, the manager will need to hire a new waiter to take care of me. In this restaurant, our waiter represents a thread. We are going to have our own waiter, our own thread, and they will handle all of our orders.Every request is single-threaded. After placing the order, the waiter will take the order to the kitchen and give it to the chef. And now the waiter just waits. He won't do anything else until the chef is finished making the food. If the user would like to order a glass of water, but he can't order anything until the chef finishes making that salad. The chef is blocking him from being able to simply order a glass of water. In this analogy, the chef represents the file system or a data store. The single thread waits for the file system to finish reading files before it can do anything else.We refer to this as blocking. Finally, salad is ready and the waiter brings the food. User can order a glass of water, and the waiter also brings that, too. The request has been served and now the manager is firing waiter because they are not needed anymore. Now, when this restaurant gets busy dinner service, every guest has their own waiter, which is pretty nice. That is pretty good service, but the waiters are mostly hanging around the kitchen and waiting for the chef to make the food. If this restaurant gets really popular, it requires a lot of space to expand because more guests mean more waiters.Now, let's take a look at another cafe. At this cafe, there is only one waiter because Node.js is single-threaded. Here, we can order some cakes. We can see that our waiter places the order for the food, then moves on to take an order from another new table. This single thread services all of the restaurant guests. That is pretty cool. When cakes are ready, the chef rings a bell, and our waiter goes and gets the crepes and delivers them to the user. He then proceeds to take another order from a new table. When their food is ready, the waiter will bring it to them as soon as he can.We can say that this waiter behaves asynchronously. Everything this waiter needs to do represents a new event, a new table, placing orders, delivering orders. These are all events, and they will get handled in the order that they are raised. Our waiter does not wait. There is no blocking. Our single waiter is busy, busy, busy, but he is killing it because he can multitask. This is what it means when we say nonblocking, event-driven IO. We have a single thread that will respond to events in the order that they are raised.This thread behaves asynchronously because it does not have to wait for resources to finish doing what they're doing before our thread can do anything else. If this cafe gets popular, we can simply franchise it. The cafe can easily be expanded by simply duplicating or forking the restaurant into a neighboring space. And this is precisely how we host Node.js applications in the cloud. Now, remember, Node.js is single-threaded. All of the users are sharing the same thread. Events are raised and recorded in an event queue and then handled in the order that they were raised.Node.js is asynchronous, which means that it can do more than one thing at a time. This ability to multitask is what makes Node.js so fast and one of the reasons so many developers are building their web applications with Node.js.Installing Node.jsIf you have not installed Node.js or you have installed an older version of Node.js awhile back, you will need to reinstall it. The easiest way to install Node.js is simply to navigate to nodejs.org in the browser and then download your package. The Node.js website will sniff out your operating system and pick the correct package for you. You can also go to the Downloads link found in the main navigation bar on the Node.js website and pick the appropriate installer for your system.Once you have a package downloaded, you can simply navigate to that file and then go ahead and open it up to run the installer. The installer will open up a wizard which you can follow through. For most people, the defaults for this wizard will be absolutely fine and agree to the license. Choose the default installation and everything should be okay. We can see that it is installed in the user/local/bin folder both Node and the Node package manager.Once you open up your terminal, you can check your Node installation by typing node -v. This will show your current version of Node.js and same thing applicable to npm –v.Node CoreIn node.js, the global object is global. Refer to https://nodejs.org/en/ to find node.js API. These are the objects that are available to us globally on the global namespace. Let's go ahead and take a look at what we can use immediately in a javascript file without having to require anything. Available to us globally, we have a console object that will allow us to log messages to the console. Before going on the example we are going to work, a suggestion to use IDE would be any tool that has inbuilt terminal [experience with command prompt is helpful]. Visual Studio Code is one such and available for free and can be downloaded from https://code.visualstudio.com/. All the below examples are available in the exercise folder.Global Object Examples:APP1: First node.js application:Create a file global.js and add console.log(‘Hello world’).Switch to the terminal (View-> Terminal ) and navigate to the location folder location where this file exists. Run ‘node global.js’Hello world should be seen as output in the terminal.Since the console is available globally, we can call global.console.log and result should be the same.APP2: Let create a variable to store this text and console.log the variable.Clear the code in global.jsAdd a variable var hello = “hello world from Node js”If you're used to using javascript in the window, you know that these variables are added to the global object. That means we should be able to see our Hello variable by typing global.hello.Console.log(global.hello);Go to terminal and run node global.jsThe output is undefined. That is because node js works a little bit different than the browser when it comes to storing variables. Let's go back to our code. Every node js file that we create is its own module. Any variable that we create in a node js file, is scoped only to that module. That means that our variables are not added to the global object the way that they are in the browserSimply remove the global prefix and run the command to see the output.Since we are coding it javascript, we should have all the javascript primitive functionality. Let's use Slice on the string.APP3:var hello = ‘Hello world from Node js’var jstNode = hello.slice(17);console.log(`${hello}` is fun to work);On running, we should see Node js is fun to work.There are many other things that are available to us globally like ‘__dirname’, ‘_filename’ to get a directory and file full path.App4: Let’s use the Path module to extract the file name only.We have require function globally available to import other modules like path.var path = require(“path”);console.log(path.basename(__filename));run node global without extension. Node assumes that all files are with .js file extension.So, the node js global object contains those objects and functions that are available to us globally like path module. Meaning, that we can start adding these objects to our node js code immediately. var path = require("path"); console.log(`Rock on World from ${path.basename(__filename)}`);Output: Rock on World from global.jsProcess arguments: Now, in the next couple of examples, we're going to continue to work with this global object, and we will discover the process object, as well, as the timing functions. Later on, we will look into common js module pattern. Which includes a module, exports, and require. App1:create a file say app.js and add console.log(process.argv);run node app.jsoutput should display default argumentsLet’s say, you want to send a few arguments and parse them accordinglyconst grab = flag => {let indexAfterFlag = process.argv.indexOf(flag) + 1; return process.argv[indexAfterFlag]; };const greeting = grab("--greeting");const user = grab("--user");console.log(`${greeting} ${user}`);run node app.js –greeting “Hello” --user “world”output should be Hello, worldIn the above example, we are using an arrow function to grab arguments and parse them accordingly.function grab(flag) {     var index = process.argv.indexOf(flag);     return (index === -1) ? null : process.argv[index+1]; } var greeting = grab('--greeting'); var user = grab('--user'); if (!user || !greeting) {     console.log("You Blew it!"); } else {     console.log(`Welcome ${user}, ${greeting}`); }Output:node app.js You Blew it! node app.js --user Tony --greeting Hello Welcome Tony, HelloStandard Input and Standard Output:App1: Let's create an example with standard input and standard output:Let’s create a file and name it as 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 outconst ask = (i = 0) => {   process.stdout.write(`\n\n\n ${questions[i]}`);   process.stdout.write(` > `); };Lets call ask methodask();Create another array to collect the answers to respective questionsconst 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 the 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()); });Let’s keep asking one question after every question until 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(); } });Let’s 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 answers. Go ${activity} ${name} you can write ${lang} code later!!! `); });Let’s start by default by asking first question.ask(0);Run node stdInOutFirst 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.var questions = [   “What is your name?",   "What would you rather be doing?",   "What is your preferred programming language?" ]; var answers = []; function ask(i) {   process.stdout.write(`\n\n ${questions[i]}`);   process.stdout.write("  > "); } 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(`Go ${answers[1]} ${answers[0]} you can finish writing ${answers[2]} later`); }); ask(0);Output:node ask  What is your name?  > Tony  What is your fav hobby?  > Programming  What is your preferred programming language?  > Javascript Go Programming Tony you can finish writing Javascript laterGlobal Timing Functions:In the last example, we started working with Node.js asynchronously by using event listeners. Another way we can work with Node.js asynchronously is through using the timing functions. The timing functions setTimeout, clearTimeout, setInterval, and clearInterval work the same way they do in the browser and are available to you globally. App1: Create a timer.js fileAdd a variable say ‘waitTime’ with 3 seconds i.e. 3000 in milliseconds.Let’s use setTimeout to wait for 3 seconds and console.log in a callback which gets executed after a delay.var waitTime = 3000; console.log(“wait for it”); setTimeout(() => console.log(“done”), waitTime);This setTimeout is going to make application wait for 3 seconds and then it will invoke the callback.Let's go to terminal and run node timer.js to observe that application waited 3 seconds after logging the first message and done.So during these three seconds while we're waiting for our application is going to be running. App2: Let's extend the above one with setInterval function.Add a couple more variables say currentTime which we can initialize it to 0 milliseconds and waitInterval with 500 (half a second).WaitInterval is used to fire the callback of setInterval every half second. var waitTime = 3000; var currentTime = 0; var waitInterval = 500; console.log(“wait for it”); setTimeout(() => console.log(“done”), waitTime);setTimeout waits for a delay once and then invokes the callback function, where as setInterval will fire the callback function over and over again on an interval time. Lets add a setInterval.var waitTime = 3000; var currentTime = 0; var waitInterval = 500; console.log(“wait for it”); setInterval(() => { currentTime =+ waitInterval; console.log(`waiting ${currentTime/1000} seconds…`); // to display in seconds }, waitInterval); setTimeout(() => console.log(“done”), waitTime);run node timer.jsWe will observe that waiting is kept incrementing by half second and once we reach 3 seconds, done is displayed.Notice that after the done log we are still going. That interval will not stop.Let’s add an exit criteria after 3 seconds and to stop application hit control C.setInterval return has to be set in a variable. And call clearInterval with argument as this variable inside setTimeOut.var waitTime = 3000; var currentTime = 0; var waitInterval = 500; console.log(“wait for it”); var interval = setInterval(() => { currentTime =+ waitInterval; console.log(`waiting ${currentTime/1000} seconds…`); }, waitInterval); setTimeout(() => { clearInterval(interval); console.log(“done”) }, waitTime);Now on running node timer.js, we will see that post 3 seconds the application is stopped.App3:Let's modify this code to display the time waiting in a percentage and also control the standard output, so that we overwrite the last line, meaning that we can see a percentage number grow.Let’s add a function for writing the percentage writeWaitingPercent. function writeWaitingPercent(percent) {     process.stdout.clearLine(); // clears the previous line     process.stdout.cursorTo(0); // moves the cursor to first line     process.stdout.write(`waiting ... ${percent}%`); // add the text to first line }Let’s add a variable to percentWaited and set to 0Call the writeWaitingPercent with percentWaited at the end of the fileAlso updated the setTimeOut to call this writeWaitingPercent with 100 in callback.setTimeout(() => { clearInterval(interval); writeWaitingPercent(100); console.log(“done”) }, waitTime);In setInterval callback also, let’s call the writeWaitingPercent based on the calculated percentWaited based on currentTime.var interval = setInterval(() => { currentTime =+ waitInterval; percentWaited = Math.floor((currentTime/waitTime) * 100); writeWaitingPercent(percentWaited); }, waitInterval);Lets run node timer in terminal observe the percentage increase. To observe a slow increase we can reduce the waitInterval to much lesser value say 50 milliseconds.var waitTime = 3000; var currentTime = 0; var waitInterval = 500; var percentWaited = 0; function writeWaitingPercent(percent) {     process.stdout.clearLine();     process.stdout.cursorTo(0);     process.stdout.write(`waiting ... ${percent}%`); } var interval = setInterval(function() {     currentTime += waitInterval;     percentWaited = Math.floor((currentTime/waitTime) * 100);     writeWaitingPercent(percentWaited); }, waitInterval); setTimeout(function() {     clearInterval(interval);     writeWaitingPercent(100);     console.log("\n\n done \n\n"); }, waitTime); writeWaitingPercent(percentWaited);Output:node timers waiting ... 100% done
logo

Node JS Tutorial

Getting Started with Node.js

The node package manager (npm) gives us access to the largest source of open source library. In this course, we are going to understand the core concepts of node.js i.e. event-driven, asynchronous, non-blocking I/O model. Also going to understand how event loop works and applications can handle many connections. We will focus on streams, the file system, the HTTP module. Before that, let's understand the core concept of node.js.

Understanding Node.js

Let us understand Node.js working model by considering two restaurants.

The first restaurant is a big, nice, fancy restaurant. In this restaurant, every new guest represents a new user, and making an order is like making a request. If I place an order for a salad, the manager will need to hire a new waiter to take care of me. In this restaurant, our waiter represents a thread. We are going to have our own waiter, our own thread, and they will handle all of our orders.

Every request is single-threaded. After placing the order, the waiter will take the order to the kitchen and give it to the chef. And now the waiter just waits. He won't do anything else until the chef is finished making the food. If the user would like to order a glass of water, but he can't order anything until the chef finishes making that salad. The chef is blocking him from being able to simply order a glass of water. In this analogy, the chef represents the file system or a data store. The single thread waits for the file system to finish reading files before it can do anything else.

We refer to this as blocking. Finally, salad is ready and the waiter brings the food. User can order a glass of water, and the waiter also brings that, too. The request has been served and now the manager is firing waiter because they are not needed anymore. Now, when this restaurant gets busy dinner service, every guest has their own waiter, which is pretty nice. That is pretty good service, but the waiters are mostly hanging around the kitchen and waiting for the chef to make the food. If this restaurant gets really popular, it requires a lot of space to expand because more guests mean more waiters.

Now, let's take a look at another cafe. At this cafe, there is only one waiter because Node.js is single-threaded. Here, we can order some cakes. We can see that our waiter places the order for the food, then moves on to take an order from another new table. This single thread services all of the restaurant guests. That is pretty cool. When cakes are ready, the chef rings a bell, and our waiter goes and gets the crepes and delivers them to the user. He then proceeds to take another order from a new table. When their food is ready, the waiter will bring it to them as soon as he can.

We can say that this waiter behaves asynchronously. Everything this waiter needs to do represents a new event, a new table, placing orders, delivering orders. These are all events, and they will get handled in the order that they are raised. Our waiter does not wait. There is no blocking. Our single waiter is busy, busy, busy, but he is killing it because he can multitask. This is what it means when we say nonblocking, event-driven IO. We have a single thread that will respond to events in the order that they are raised.

This thread behaves asynchronously because it does not have to wait for resources to finish doing what they're doing before our thread can do anything else. If this cafe gets popular, we can simply franchise it. The cafe can easily be expanded by simply duplicating or forking the restaurant into a neighboring space. And this is precisely how we host Node.js applications in the cloud. Now, remember, Node.js is single-threaded. All of the users are sharing the same thread. Events are raised and recorded in an event queue and then handled in the order that they were raised.

Node.js is asynchronous, which means that it can do more than one thing at a time. This ability to multitask is what makes Node.js so fast and one of the reasons so many developers are building their web applications with Node.js.

Installing Node.js

If you have not installed Node.js or you have installed an older version of Node.js awhile back, you will need to reinstall it. The easiest way to install Node.js is simply to navigate to nodejs.org in the browser and then download your package. The Node.js website will sniff out your operating system and pick the correct package for you. You can also go to the Downloads link found in the main navigation bar on the Node.js website and pick the appropriate installer for your system.

Once you have a package downloaded, you can simply navigate to that file and then go ahead and open it up to run the installer. The installer will open up a wizard which you can follow through. For most people, the defaults for this wizard will be absolutely fine and agree to the license. Choose the default installation and everything should be okay. We can see that it is installed in the user/local/bin folder both Node and the Node package manager.

Once you open up your terminal, you can check your Node installation by typing node -v. This will show your current version of Node.js and same thing applicable to npm –v.

Node Core

In node.js, the global object is global. Refer to https://nodejs.org/en/ to find node.js API. These are the objects that are available to us globally on the global namespace. Let's go ahead and take a look at what we can use immediately in a javascript file without having to require anything. 

Available to us globally, we have a console object that will allow us to log messages to the console. 

Before going on the example we are going to work, a suggestion to use IDE would be any tool that has inbuilt terminal [experience with command prompt is helpful]. Visual Studio Code is one such and available for free and can be downloaded from https://code.visualstudio.com/. All the below examples are available in the exercise folder.

Global Object Examples:

APP1: 

First node.js application:

  • Create a file global.js and add console.log(‘Hello world’).
  • Switch to the terminal (View-> Terminal ) and navigate to the location folder location where this file exists. 
  • Run ‘node global.js’
  • Hello world should be seen as output in the terminal.

Since the console is available globally, we can call global.console.log and result should be the same.

APP2: 

Let create a variable to store this text and console.log the variable.

  • Clear the code in global.js
  • Add a variable var hello = “hello world from Node js”
  • If you're used to using javascript in the window, you know that these variables are added to the global object. That means we should be able to see our Hello variable by typing global.hello.
  • Console.log(global.hello);
  • Go to terminal and run node global.js
  • The output is undefined. That is because node js works a little bit different than the browser when it comes to storing variables. Let's go back to our code. Every node js file that we create is its own module. Any variable that we create in a node js file, is scoped only to that module. That means that our variables are not added to the global object the way that they are in the browser
  • Simply remove the global prefix and run the command to see the output.

Since we are coding it javascript, we should have all the javascript primitive functionality. Let's use Slice on the string.

APP3:

  • var hello = ‘Hello world from Node js’
  • var jstNode = hello.slice(17);
  • console.log(`${hello}` is fun to work);
  • On running, we should see Node js is fun to work.
  • There are many other things that are available to us globally like ‘__dirname’, ‘_filename’ to get a directory and file full path.

App4:

 Let’s use the Path module to extract the file name only.

  • We have require function globally available to import other modules like path.
  • var path = require(“path”);
  • console.log(path.basename(__filename));
  • run node global without extension. Node assumes that all files are with .js file extension.

So, the node js global object contains those objects and functions that are available to us globally like path module. Meaning, that we can start adding these objects to our node js code immediately. 

var path = require("path");
console.log(`Rock on World from ${path.basename(__filename)}`);
Output:
Rock on World from global.js

Process arguments: 

Now, in the next couple of examples, we're going to continue to work with this global object, and we will discover the process object, as well, as the timing functions. Later on, we will look into common js module pattern. Which includes a module, exports, and require. App1:

  • create a file say app.js and add console.log(process.argv);
  • run node app.js
  • output should display default arguments

Let’s say, you want to send a few arguments and parse them accordingly

  • const grab = flag => {
let indexAfterFlag = process.argv.indexOf(flag) + 1;
return process.argv[indexAfterFlag];
};
  • const greeting = grab("--greeting");
  • const user = grab("--user");
  • console.log(`${greeting} ${user}`);
  • run node app.js –greeting “Hello” --user “world”
  • output should be Hello, world

In the above example, we are using an arrow function to grab arguments and parse them accordingly.

function grab(flag) {
    var index = process.argv.indexOf(flag);
    return (index === -1) ? null : process.argv[index+1];
}
var greeting = grab('--greeting');
var user = grab('--user');
if (!user || !greeting) {
    console.log("You Blew it!");
} else {
    console.log(`Welcome ${user}, ${greeting}`);
}

Output:

node app.js
You Blew it!
node app.js --user Tony --greeting Hello
Welcome Tony, Hello

Standard Input and Standard Output:

App1: Let's create an example with standard input and standard output:

  • Let’s create a file and name it as 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 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 the 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());
});
  • Let’s keep asking one question after every question until 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();
}
});
  • Let’s 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 answers. Go ${activity} ${name} you can write ${lang} code later!!! `);
});
  • Let’s 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.
var questions = [
  “What is your name?",
  "What would you rather be doing?",
  "What is your preferred programming language?"
];
var answers = [];
function ask(i) {
  process.stdout.write(`\n\n ${questions[i]}`);
  process.stdout.write("  > ");
}
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(`Go ${answers[1]} ${answers[0]} you can finish writing ${answers[2]} later`);
});
ask(0);

Output:

node ask
 What is your name?  > Tony
 What is your fav hobby?  > Programming
 What is your preferred programming language?  > Javascript
Go Programming Tony you can finish writing Javascript later

Global Timing Functions:

In the last example, we started working with Node.js asynchronously by using event listeners. Another way we can work with Node.js asynchronously is through using the timing functions. The timing functions setTimeout, clearTimeout, setInterval, and clearInterval work the same way they do in the browser and are available to you globally. 

App1: 

  • Create a timer.js file
  • Add a variable say ‘waitTime’ with 3 seconds i.e. 3000 in milliseconds.
  • Let’s use setTimeout to wait for 3 seconds and console.log in a callback which gets executed after a delay.
var waitTime = 3000;
console.log(“wait for it”);
setTimeout(() => console.log(“done”), waitTime);
  • This setTimeout is going to make application wait for 3 seconds and then it will invoke the callback.
  • Let's go to terminal and run node timer.js to observe that application waited 3 seconds after logging the first message and done.
  • So during these three seconds while we're waiting for our application is going to be running. 

App2: 

  • Let's extend the above one with setInterval function.
  • Add a couple more variables say currentTime which we can initialize it to 0 milliseconds and waitInterval with 500 (half a second).
  • WaitInterval is used to fire the callback of setInterval every half second. 
var waitTime = 3000;
var currentTime = 0;
var waitInterval = 500;
console.log(“wait for it”);
setTimeout(() => console.log(“done”), waitTime);
  • setTimeout waits for a delay once and then invokes the callback function, where as setInterval will fire the callback function over and over again on an interval time. Lets add a setInterval.
var waitTime = 3000;
var currentTime = 0;
var waitInterval = 500;
console.log(“wait for it”);
setInterval(() => {
currentTime =+ waitInterval;
console.log(`waiting ${currentTime/1000} seconds…`); // to display in seconds
}, waitInterval);
setTimeout(() => console.log(“done”), waitTime);
  • run node timer.js
  • We will observe that waiting is kept incrementing by half second and once we reach 3 seconds, done is displayed.
  • Notice that after the done log we are still going. That interval will not stop.
  • Let’s add an exit criteria after 3 seconds and to stop application hit control C.
  • setInterval return has to be set in a variable. And call clearInterval with argument as this variable inside setTimeOut.
var waitTime = 3000;
var currentTime = 0;
var waitInterval = 500;
console.log(“wait for it”);
var interval = setInterval(() => {
currentTime =+ waitInterval;
console.log(`waiting ${currentTime/1000} seconds…`);
}, waitInterval);
setTimeout(() => {
clearInterval(interval);
console.log(“done”)
}, waitTime);
  • Now on running node timer.js, we will see that post 3 seconds the application is stopped.

App3:

Let's modify this code to display the time waiting in a percentage and also control the standard output, so that we overwrite the last line, meaning that we can see a percentage number grow.

  • Let’s add a function for writing the percentage writeWaitingPercent. 
function writeWaitingPercent(percent) {
    process.stdout.clearLine(); // clears the previous line
    process.stdout.cursorTo(0); // moves the cursor to first line
    process.stdout.write(`waiting ... ${percent}%`); // add the text to first line
}
  • Let’s add a variable to percentWaited and set to 0
  • Call the writeWaitingPercent with percentWaited at the end of the file
  • Also updated the setTimeOut to call this writeWaitingPercent with 100 in callback.
setTimeout(() => {
clearInterval(interval);
writeWaitingPercent(100);
console.log(“done”)
}, waitTime);
  • In setInterval callback also, let’s call the writeWaitingPercent based on the calculated percentWaited based on currentTime.
var interval = setInterval(() => {
currentTime =+ waitInterval;
percentWaited = Math.floor((currentTime/waitTime) * 100);
writeWaitingPercent(percentWaited);
}, waitInterval);
  • Lets run node timer in terminal observe the percentage increase. 
  • To observe a slow increase we can reduce the waitInterval to much lesser value say 50 milliseconds.
var waitTime = 3000;
var currentTime = 0;
var waitInterval = 500;
var percentWaited = 0;
function writeWaitingPercent(percent) {
    process.stdout.clearLine();
    process.stdout.cursorTo(0);
    process.stdout.write(`waiting ... ${percent}%`);
}
var interval = setInterval(function() {
    currentTime += waitInterval;
    percentWaited = Math.floor((currentTime/waitTime) * 100);
    writeWaitingPercent(percentWaited);
}, waitInterval);
setTimeout(function() {
    clearInterval(interval);
    writeWaitingPercent(100);
    console.log("\n\n done \n\n");
}, waitTime);
writeWaitingPercent(percentWaited);

Output:

node timers
waiting ... 100%
done

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