Chapter 1
Chapter 1
Advantages of Node JS
One of the key advantages of Node.js is that developers find it easy to scale the applications in horizontal as
well as the vertical directions. The applications can be scaled in horizontal manner by the addition of
additional nodes to the existing system.
Moreover, Node.js also offers you the option of adding extra resources to the single nodes during the vertical
scaling of the application. So, it is highly scalable and provides better option than other JavaScript servers.
2. Easy to Learn
Since JavaScript is one of the most popular programming languages, most of the front-end developers have
a good grasp over it.
It becomes much easier for them to start using the Node.js at the backend. It is easier to learn Node.js and
consumes less time to work with it.
Node.js offers the developers the luxury of writing the server-side applications in the JavaScript. This allows
the Node.js developers to write both the front-end as well as the back-end web application in JavaScript
using a runtime environment.
And they don’t need to use any other server-side programming language. It also makes the deployment of
the web applications simpler because almost all the web browsers support JavaScript.
Node.js has been regarded as a full-stack JavaScript for serving both the client and the server-side
applications.
Therefore, the advantage is that you don’t have to hire separate developers for backend as well as the
front-end development. It saves both your valuable money and time.
It has been mentioned earlier that Node.js interprets the JavaScript code via Google’s V8 JavaScript engine.
This engine complies the JavaScript code directly into the machine code. This makes it easier and faster to
implement the code in a effective manner.
The speed of the code execution also enhanced by runtime environment as it supports the non-blocking I/O
operations.
Node.js is blessed to have a large and active community of developers who keep on continuously
contributing towards its further development and improvement.
In fact, the groups of developers are well supported by the JavaScript programmers providing ready-made
and easy solutions and codes in GitHub. It is expected that the developers will initiate many further
developers in the future.
The open-source runtime environment of the Node.js also provides the facility of caching single modules.
Whenever there is any request for the first module, it gets cached in the application memory.
The developers don’t have to re-execute the codes as caching allows applications to load the web pages
faster and responds more swiftly to the user.
Another advantage that Node.js offers to the developers is the freedom to develop the apps and software.
This is one essential feature, which remains absent in Ruby on Rails imposing certain guidelines. You can
begin everything from the scratch while developing applications.
With Node.js, the developers can get an extended support for the various commonly used tools. Let’s take an
example. Suppose, you want to test the source code of Node.js application; you can do so by using the
Jasmin and other such unit-testing tools.
Similarly, if you want to identify and install the project dependencies, you can make use of npm, a powerful
package manager. You can use grunt for task running of the project.
Since the Node.js is providing the option of non-blocking I/O systems, it relatively helps you to process
several requests concurrently.
The system can handle the concurrent request handling efficiently better than others including Ruby or
Python. The incoming requests get lined up and are executed quickly and systematically.
11. Node.js is Highly Extensible
The Node.js is known to be highly extensible, which means that you can customize and further extend
Node.js as per their requirements.
You can also make use of JSON to provide the scope for exchange of data between the web server and the
client. It also is facilitated with built-in APIs for developing HTTP, TCP, and DNS etc. servers.
Well, Node.js also has some cons which need to be necessarily discussed.
One of the key problems that most of the developers encounter is the Application Programming Interface
(API) keeps on changing at frequent intervals and does not remain stable.
At times, a new API appears having a number of backwards-incompatible changes. As a result the
developers are forced to make changes in the accessible code bases to match the compatibility with the latest
version of the Node.js API.
The JavaScript does not have a well equipped and robust library system in comparison to other
programming languages.
The result is that the users are forced to take the support of common library for executing various tasks such
as Object-Relational Mapping (ORM), processing of the images, handling database operations, and XML
parsing etc.
This makes it difficult for the developers to even implement the common programming tasks using Node.js.
If you want to make the applications more scalable, the necessary requisite is adoption of the asynchronous
programming model.
However, many developers may find this programming model to be more difficult in comparison to the
linear blocking I/O programming.
Another con of the asynchronous programming is the codes tend to become clumsy and the programmers
have to depend on the nested calls.
What is Node.js?
Node.js is an open-source, JavaScript run-time environment that helps in the execution of JavaScript code
outside of a browser.
In the traditional web server model, each request is handled by a dedicated thread from the thread pool. If no
thread is available in the thread pool at any point of time then the request waits till the next available thread.
Dedicated thread executes a particular request and does not return to thread pool until it completes the
execution and returns a response.
Node.js processes user requests differently when compared to a traditional web server model. Node.js runs
in a single process and the application code runs in a single thread and thereby needs less resources than
other platforms. All the user requests to your web application will be handled by a single thread and all the
I/O work or long running job is performed asynchronously for a particular request. So, this single thread
doesn't have to wait for the request to complete and is free to handle the next request. When asynchronous
I/O work completes then it processes the request further and sends the response.
An event loop is constantly watching for the events to be raised for an asynchronous job and executing
callback function when the job completes. Internally, Node.js uses libev for the event loop which in turn
uses internal C++ thread pool to provide asynchronous I/O.
The following figure illustrates asynchronous web server model using Node.js.
Node.js Process Model
Node.js process model increases the performance and scalability with a few caveats. Node.js is not fit for an
application which performs CPU-intensive operations like image processing or other heavy computation
work because it takes time to process a request and thereby blocks the single thread
Node.js development environment can be setup in Windows, Mac, Linux and Solaris. The following
tools/SDK are required for developing a Node.js application on any platform.
1. Node.js
2. Node Package Manager (NPM)
3. IDE (Integrated Development Environment) or TextEditor
NPM (Node Package Manager) is included in Node.js installation since Node version 0.6.0., so there is no
need to install it separately.
Visit Node.js official web site https://nodejs.org. It will automatically detect OS and display download link
as per your Operating System. For example, it will display following download link for 64 bit Windows OS.
Download Node.JS Installer for Windows
Download the installer for windows by clicking on LTS or Current version button. Here, we will install the
latest version LTS for windows that has long time support. However, you can also install the Current version
which will have the latest features.
After you download the MSI, double-click on it to start the installation as shown below.
Node.js
Installation
Click Next to read and accept the License Agreement and then click Install. It will install Node.js quickly on
your computer. Finally, click finish to complete the installation.
Verify Installation
Once you install Node.js on your computer, you can verify it by opening the command prompt and
typing node -v. If Node.js is installed successfully then it will display the version of the Node.js installed on
your machine, as shown below.
Working in REPL
Node.js comes with virtual environment called REPL (aka Node shell). REPL stands for
Read-Eval-Print-Loop. It is a quick and easy way to test simple Node.js/JavaScript code.
To launch the REPL (Node shell), open command prompt (in Windows) or terminal (in Mac or
UNIX/Linux) and type node as shown below. It will change the prompt to > in Windows and MAC.
You can now test pretty much any Node.js/JavaScript expression in REPL. 10 + 20 will display 30
immediately in new line.
You can also define variables and perform some operation on them.
If you need to write multi line JavaScript expression or function then just press Enter whenever you want to
write something in the next line as a continuation of your code. The REPL terminal will display three dots
(...), it means you can continue on next line. Write .break to get out of continuity mode.
For example, you can define a function and execute it as shown below.
You can execute an external JavaScript file by executing the node fileName command. For example, the
following runs mynodejs-app.js on the command prompt/terminal and displays the result.
mynodejs-app.js
Copy
console.log("Hello World");
Now, you can execute mynodejs-app from command prompt as shown below.
Run
External JavaScript file
To exit from the REPL terminal, press Ctrl + C twice or write .exit and press Enter.
Thus, you can execute any Node.js/JavaScript code in the node shell (REPL). This will give you a result
which is similar to the one you will get in the console of Google Chrome browser.
Note:
ECMAScript implementation in Node.js and browsers is slightly different. For example, {}+{} is '[object
Object][object Object]' in Node.js REPL, whereas the same code is NaN in the Chrome console because of
the automatic semicolon insertion feature. However, mostly Node.js REPL and the Chrome/Firefox consoles
are similar.