Node.js is an event-driven, server-side JavaScript environment. Node.js runs JS using the V8 engine developed by Google for use in their Chrome web browser. Leveraging V8 allows Node.js to provide a server-side runtime environment that compiles and executes JS at lightning speeds.
Node.js runs as a single-threaded process that acts upon callbacks and never blocks on the main thread, making it high-performing for web applications. A callback is basically a function that is passed to another function so that it can be called once that function is done. We will look into this in a later topic. This is known as the single-threaded event loop model. Other web technologies mainly follow the multithreaded request-response architecture.
The following diagram depicts the architecture of Node.js. As you can see, it's mostly C++ wrapped by a JavaScript layer. We will not go over the details of each component, since that is out of the scope of this chapter.

Node's goal is to offer an easy and safe way to build high-performance and scalable network applications in JavaScript.
Node.js has the following four major applications:
- Creating REST APIs: We are going to look into this more in subsequent chapters
- Creating real-time services: Because of Node's asynchronous event-driven programming, it is well-suited to reactive real-time services
- Building microservices: Since Node.js has a very lean core, it is best suited to building microservices, since you will only add dependencies that you actually need for the microservices, as opposed to the glut that comes with other frameworks
- Tooling: For example, DevOps automations, and so on
Before You Begin
Open the IDE and the Terminal to implement this solution.
Aim
Learn how to write a basic Node.js file and run it.
Scenario
You are writing a very basic mathematical library with handy mathematical functions.
Steps for Completion
- Create your project directory (folder), where all the code for this and other chapters will be kept. You can call it
beginning-nodejs
for brevity. Inside this directory, create another directory namedlesson-1
, and inside that, create another directory calledactivity-a
. All this can be done using the following command:
mkdir -p beginning-nodejs/lesson-1/activity-a
- Inside
activity-a
, create a file usingtouch maths.js
command. - Inside this file, create the following functions:
add
: This takes any two numbers and returns the sum of both, for example,add(2, 5)
returns7
sum
: Unlikeadd
, takes any number of numbers and returns their sum, for example,sum(10, 5, 6)
returns21
- After these functions, write the following code to act as tests for your code:
console.log(add(10, 6)); // 16 console.log(sum(10, 5, 6)); // 21
- Now, on the Terminal, change directory to
lesson-1
. That's where we will be running most of our code from for the whole chapter. - To run the code, run the following command:
node activity-a/math.js
The 16
and 21
values should be printed out on the Terminal.
Note
Even though you can configure the IDE so that Node.js code be run at the click of a button, it's strongly recommend that you run the code from the Terminal to appreciate how Node.js actually works.
For uniformity, if you are using a Windows machine, then run your commands from the Git Bash Terminal. For the reference solution, use the math.js
file at Code/Lesson-1/activity-solutions/activity-a
.