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.
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.
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.
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:
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.
Since we are coding it javascript, we should have all the javascript primitive functionality. Let's use Slice on the string.
APP3:
App4:
Let’s use the Path module to extract the file name only.
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
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:
Let’s say, you want to send a few arguments and parse them accordingly
let indexAfterFlag = process.argv.indexOf(flag) + 1; return process.argv[indexAfterFlag]; };
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:
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();
const answers = [];
process.stdin.on("data", data => { });
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 answers. Go ${activity} ${name} you can write ${lang} code later!!! `); });
ask(0);
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:
var waitTime = 3000; console.log(“wait for it”); setTimeout(() => console.log(“done”), waitTime);
App2:
var waitTime = 3000; var currentTime = 0; var waitInterval = 500; console.log(“wait for it”); setTimeout(() => console.log(“done”), waitTime);
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);
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);
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.
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 }
setTimeout(() => { clearInterval(interval); writeWaitingPercent(100); console.log(“done”) }, waitTime);
var interval = setInterval(() => { currentTime =+ waitInterval; percentWaited = Math.floor((currentTime/waitTime) * 100); writeWaitingPercent(percentWaited); }, waitInterval);
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
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 *