
Domains
Agile Management
Master Agile methodologies for efficient and timely project delivery.
View All Agile Management Coursesicon-refresh-cwCertifications
Scrum Alliance
16 Hours
Best Seller
Certified ScrumMaster (CSM) CertificationScrum Alliance
16 Hours
Best Seller
Certified Scrum Product Owner (CSPO) CertificationScaled Agile
16 Hours
Trending
Leading SAFe 6.0 CertificationScrum.org
16 Hours
Professional Scrum Master (PSM) CertificationScaled Agile
16 Hours
SAFe 6.0 Scrum Master (SSM) CertificationAdvanced Certifications
Scaled Agile, Inc.
32 Hours
Recommended
Implementing SAFe 6.0 (SPC) CertificationScaled Agile, Inc.
24 Hours
SAFe 6.0 Release Train Engineer (RTE) CertificationScaled Agile, Inc.
16 Hours
Trending
SAFe® 6.0 Product Owner/Product Manager (POPM)IC Agile
24 Hours
ICP Agile Certified Coaching (ICP-ACC)Scrum.org
16 Hours
Professional Scrum Product Owner I (PSPO I) TrainingMasters
32 Hours
Trending
Agile Management Master's Program32 Hours
Agile Excellence Master's ProgramOn-Demand Courses
Agile and ScrumRoles
Scrum MasterTech Courses and Bootcamps
Full Stack Developer BootcampAccreditation Bodies
Scrum AllianceTop Resources
Scrum TutorialProject Management
Gain expert skills to lead projects to success and timely completion.
View All Project Management Coursesicon-standCertifications
PMI
36 Hours
Best Seller
Project Management Professional (PMP) CertificationAxelos
32 Hours
PRINCE2 Foundation & Practitioner CertificationAxelos
16 Hours
PRINCE2 Foundation CertificationAxelos
16 Hours
PRINCE2 Practitioner CertificationSkills
Change ManagementMasters
Job Oriented
45 Hours
Trending
Project Management Master's ProgramUniversity Programs
45 Hours
Trending
Project Management Master's ProgramOn-Demand Courses
PRINCE2 Practitioner CourseRoles
Project ManagerAccreditation Bodies
PMITop Resources
Theories of MotivationCloud Computing
Learn to harness the cloud to deliver computing resources efficiently.
View All Cloud Computing Coursesicon-cloud-snowingCertifications
AWS
32 Hours
Best Seller
AWS Certified Solutions Architect - AssociateAWS
32 Hours
AWS Cloud Practitioner CertificationAWS
24 Hours
AWS DevOps CertificationMicrosoft
16 Hours
Azure Fundamentals CertificationMicrosoft
24 Hours
Best Seller
Azure Administrator CertificationMicrosoft
45 Hours
Recommended
Azure Data Engineer CertificationMicrosoft
32 Hours
Azure Solution Architect CertificationMicrosoft
40 Hours
Azure DevOps CertificationAWS
24 Hours
Systems Operations on AWS Certification TrainingAWS
24 Hours
Developing on AWSMasters
Job Oriented
48 Hours
New
AWS Cloud Architect Masters ProgramBootcamps
Career Kickstarter
100 Hours
Trending
Cloud Engineer BootcampRoles
Cloud EngineerOn-Demand Courses
AWS Certified Developer Associate - Complete GuideAuthorized Partners of
AWSTop Resources
Scrum TutorialIT Service Management
Understand how to plan, design, and optimize IT services efficiently.
View All DevOps Coursesicon-git-commitCertifications
Axelos
16 Hours
Best Seller
ITIL 4 Foundation CertificationAxelos
16 Hours
ITIL Practitioner CertificationPeopleCert
16 Hours
ISO 14001 Foundation CertificationPeopleCert
16 Hours
ISO 20000 CertificationPeopleCert
24 Hours
ISO 27000 Foundation CertificationAxelos
24 Hours
ITIL 4 Specialist: Create, Deliver and Support TrainingAxelos
24 Hours
ITIL 4 Specialist: Drive Stakeholder Value TrainingAxelos
16 Hours
ITIL 4 Strategist Direct, Plan and Improve TrainingOn-Demand Courses
ITIL 4 Specialist: Create, Deliver and Support ExamTop Resources
ITIL Practice TestData Science
Unlock valuable insights from data with advanced analytics.
View All Data Science Coursesicon-dataBootcamps
Job Oriented
6 Months
Trending
Data Science BootcampJob Oriented
289 Hours
Data Engineer BootcampJob Oriented
6 Months
Data Analyst BootcampJob Oriented
288 Hours
New
AI Engineer BootcampSkills
Data Science with PythonRoles
Data ScientistOn-Demand Courses
Data Analysis Using ExcelTop Resources
Machine Learning TutorialDevOps
Automate and streamline the delivery of products and services.
View All DevOps Coursesicon-terminal-squareCertifications
DevOps Institute
16 Hours
Best Seller
DevOps Foundation CertificationCNCF
32 Hours
New
Certified Kubernetes AdministratorDevops Institute
16 Hours
Devops LeaderSkills
KubernetesRoles
DevOps EngineerOn-Demand Courses
CI/CD with Jenkins XGlobal Accreditations
DevOps InstituteTop Resources
Top DevOps ProjectsBI And Visualization
Understand how to transform data into actionable, measurable insights.
View All BI And Visualization Coursesicon-microscopeBI and Visualization Tools
Certification
24 Hours
Recommended
Tableau CertificationCertification
24 Hours
Data Visualization with Tableau CertificationMicrosoft
24 Hours
Best Seller
Microsoft Power BI CertificationTIBCO
36 Hours
TIBCO Spotfire TrainingCertification
30 Hours
Data Visualization with QlikView CertificationCertification
16 Hours
Sisense BI CertificationOn-Demand Courses
Data Visualization Using Tableau TrainingTop Resources
Python Data Viz LibsCyber Security
Understand how to protect data and systems from threats or disasters.
View All Cyber Security Coursesicon-refresh-cwCertifications
CompTIA
40 Hours
Best Seller
CompTIA Security+EC-Council
40 Hours
Certified Ethical Hacker (CEH v12) CertificationISACA
22 Hours
Certified Information Systems Auditor (CISA) CertificationISACA
40 Hours
Certified Information Security Manager (CISM) Certification(ISC)²
40 Hours
Certified Information Systems Security Professional (CISSP)(ISC)²
40 Hours
Certified Cloud Security Professional (CCSP) Certification16 Hours
Certified Information Privacy Professional - Europe (CIPP-E) CertificationISACA
16 Hours
COBIT5 Foundation16 Hours
Payment Card Industry Security Standards (PCI-DSS) CertificationOn-Demand Courses
CISSPTop Resources
Laptops for IT SecurityWeb Development
Learn to create user-friendly, fast, and dynamic web applications.
View All Web Development Coursesicon-codeBootcamps
Career Kickstarter
6 Months
Best Seller
Full-Stack Developer BootcampJob Oriented
3 Months
Best Seller
UI/UX Design BootcampEnterprise Recommended
6 Months
Java Full Stack Developer BootcampCareer Kickstarter
490+ Hours
Front-End Development BootcampCareer Accelerator
4 Months
Backend Development Bootcamp (Node JS)Skills
ReactOn-Demand Courses
Angular TrainingTop Resources
Top HTML ProjectsBlockchain
Understand how transactions and databases work in blockchain technology.
View All Blockchain Coursesicon-stop-squareBlockchain Certifications
40 Hours
Blockchain Professional Certification32 Hours
Blockchain Solutions Architect Certification32 Hours
Blockchain Security Engineer Certification24 Hours
Blockchain Quality Engineer Certification5+ Hours
Blockchain 101 CertificationOn-Demand Courses
NFT Essentials 101: A Beginner's GuideTop Resources
Blockchain Interview QsProgramming
Learn to code efficiently and design software that solves problems.
View All Programming Coursesicon-codeSkills
Python CertificationInterview Prep
Career Accelerator
3 Months
Software Engineer Interview PrepOn-Demand Courses
Data Structures and Algorithms with JavaScriptTop Resources
Python TutorialThe 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 in 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
- 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