Node.js is a very powerful JavaScript based framework/platform built on Google Chrome’s JavaScript V8 Engine. It is used to delevop I/O intensive web applications like video streaming sites, single page applications and other web application. Node.js is open source, completely free, and used by thousands of developers around the world.

What is Node.js?

Node.js is a server side platform built on Google Chrome’s JavaScript Engine(V8 Engine). Node.js was developed by Ryan Dahl in 2009 and it’s latest version is v0.10.36. Defintion of Node.js as put by its official documentation is as follows:

[callout font_size=”13px” style=”vistablue”]Node.js® is a platform built on Chrome’s JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices..[/callout]

Node.js is an open source, cross-platform Runtime Environment for server-side and networking applications. Node.js applications are written in JavaScript, and can be run within the Node.js runtime on OS X, Microsoft Windows, and Linux.

Node.js also provides a rich library of various JavaScript modules which eases the developement of web application using Node.js to a great extents.

Features of Node.js

Following are few of the important features which are making Node.js as the first choice of software architects.

  • Aynchronous and Event Driven All APIs of Node.js library are aynchronous that is non-blocking. It essentially means a Node.js based server never waits for a API to return data. Server moves to next API after calling it and a notification mechanism of Events of Node.js helps server to get response from the previous API call.
  • Very Fast Being built on Google Chrome’s V8 JavaScript Engine, Node.js library is very fast in code execution.
  • Single Threaded but highly Scalable – Node.js uses a single threaded model with event looping. Event mechanism helps server to respond in a non-bloking ways and makes server highly scalable as opposed to traditional servers which create limited threads to handle requests. Node.js uses a single threaded program and same program can services much larger number of requests than traditional server like Apache HTTP Server.
  • No Buffering – Node.js applications never buffer any data. These applications simply output the data in chunks.
  • License – Node.js is released under the MIT license.

Who Uses Node.js?

Following is the link on github wiki containing an exhaustive list of projects, application and companies which are using Node.js. This list include eBay, General Electric, GoDaddy, Microsoft, PayPal, Uber, Wikipins, Yahoo!, Yammer and the list continues.

Concepts

The following diagram depicts some important parts of Node.js which we will discuss in detail in the subsequent chapters.

Node.js Concepts

Where to Use Node.js?

Following are the areas where Node.js is proving itself a perfect technology partner.

  • I/O bound Applications
  • Data Streaming Applications
  • Data Intensive Realtime Applications (DIRT)
  • JSON APIs based Applications
  • Single Page Applications

Where Not to Use Node.js?

It is not advisable to use Node.js for CPU intensive applications.

Local Environment Setup

If you are willing to set up your environment for Node.js, you need the following two softwares available on your computer, (a) Text Editor and (b) The Node.js binary installables.

Text Editor

This will be used to type your program. Examples of few editors include Windows Notepad, OS Edit command, Brief, Epsilon, EMACS, and vim or vi.

Name and version of text editor can vary on different operating systems. For example, Notepad will be used on Windows, and vim or vi can be used on windows as well as Linux or UNIX.

The files you create with your editor are called source files and contain program source code. The source files for Node.js programs are typically named with the extension “.js“.

Before starting your programming, make sure you have one text editor in place and you have enough experience to write a computer program, save it in a file, and finally execute it.

The Node.js Runtime

The source code written in source file is simply javascript. The Node.js interprter will be used to interpret and execute your javascript code.

Node.js distribution comes as a binary installable for SunOS , Linux, Mac OS X, and Windows operating systems with the 32-bit (386) and 64-bit (amd64) x86 processor architectures.

Following section guides you on how to install Node.js binary distribution on various OS.

Download Node.js archive

Download latest version of Node.js installable archive file from Node.js Downloads. At the time of writing this tutorial, following are the versions available on different OS.

OS Archive name
Windows node-v0.12.0-x64.msi
Linux node-v0.12.0-linux-x86.tar.gz
Mac node-v0.12.0-darwin-x86.tar.gz
SunOS node-v0.12.0-sunos-x86.tar.gz

Installation on UNIX/Linux/Mac OS X, and SunOS

Based on your OS architecture, donwload and extract the archive node-v0.12.0-osname.tar.gz into /tmp, and then finally move extracted files into /usr/local/nodejs directory. For example:

Add /usr/local/nodejs/bin to the PATH environment variable.

OS Output
Linux export PATH=$PATH:/usr/local/nodejs/bin
Mac export PATH=$PATH:/usr/local/nodejs/bin
FreeBSD export PATH=$PATH:/usr/local/nodejs/bin

Installation on Windows

Use the MSI file and follow the prompts to install the Node.js. By default, the installer uses the Node.js distribution in C:Program Filesnodejs. The installer should set the C:Program Filesnodejsbin directory in window’s PATH environment variable. Restart any open command prompts for the change to take effect.

Verify installation: Executing a File

Create a js file named main.js on your machine (Windows or Linux) having the following code.

Now execute main.js file using Node.js interpreter to see the result:

If everything is fine with your installation, this should produce the following result:

Before creating actual “Hello, World!” application using Node.js, let us see the parts of a Node.js application. A Node.js application consists of following three important parts:

  1. Import required modules: We use require directive to load a Node.js module.
  2. Create server: A server which will listen to client’s request similar to Apache HTTP Server.
  3. Read request and return response: server created in earlier step will read HTTP request made by client which can be a browser or console and return the response.

Creating Node.js Application

Step 1 – Import required module

We use require directive to load http module and store returned HTTP instance into http variable as follows:

Step 2: Create Server

At next step we use created http instance and call http.createServer() method to create server instance and then we bind it at port 8081 using listen method associated with server instance. Pass it a function with parameters request and response. Write the sample implementation to always return “Hello World”.

Above code is enough to create an HTTP server which listens ie. wait for a reuest over 8081 port on local machine.

Step 3: Testing Request & Response

Let’s put step 1 and 2 together in a file called main.js and start our HTTP server as shown below:

Now execute the main.js to start the server as follows:

Verify the Output. Server has started

Make a request to Node.js server

Open http://127.0.0.1:8081/ in any browser and see the below result.

First Application

Congratulations, you have your first HTTP server up and running which is responding all the HTTP requests at port 8081.

 

REPL stands for Read Eval Print Loop and it represents a computer environment like a window console or Unix/Linux shell where a command is entered and system responds with an output in interactive mode. Node.js or Node comes bundled with a REPL environment. It performs the following desired tasks.

  • Read – Reads user’s input, parse the input into JavaScript data-structure and stores in memory.
  • Eval – Takes and evaluates the data structure
  • Print – Prints the result
  • Loop – Loops the above command until user press ctrl-c twice.

REPL feature of Node is very useful in experimenting with Node.js codes and to debug JavaScript codes.

Online REPL Terminal

To simplify your learning, we have setup required easy to use Node.js REPL environment online, where you can practice Node.js syntax: Launch Node.js REPL Terminal

Starting REPL

REPL can be started by simply running node on shell/console without any argument as follows.

You will see the REPL Command prompt > where you can type any Node.js command:

Simple Expression

Let’s try simple mathematics at Node.js REPL command prompt:

Use Variables

You can make use variables to store values and print later like any conventional script. If var keyword is not used then value is stored in the variable and printed. Whereas if var keyword is used then value is stored but not printed. You can print variables usind console.log().

Multiline Expression

Node REPL supports multiline expression similar to JavaScript. Let’s check the following do-while loop in action:

comes automatically when you press enters after opening bracket. Node automatically checks the continuity of expressions.

Underscore Variable

You can use undercore _ to get the last result:

REPL Commands

  • ctrl + c – terminate the current command.
  • ctrl + c twice – terminate the Node REPL.
  • ctrl + d – terminate the Node REPL.
  • Up/Down Keys – see command history and modify previous commands.
  • tab Keys – list of current commands.
  • .help – list of all commands.
  • .break – exit from multiline expression.
  • .clear – exit from multiline expression
  • .save filename – save current Node REPL session to a file.
  • .load filename – load file content in current Node REPL session.

Stopping REPL

As mentioned above you will need to use ctrl + c twice command to come out of Node.js REPL.

Node Package Manager (npm) provides following two main functionalities:

  • Online repositories for node.js packages/modules which are searchable on search.nodejs.org
  • Command line utility to install Node.js packages, do version management and dependency management of Node.js packages.

npm comes bundled with Node.js installables after v0.6.3 version. To verify the same, open console and type following command and see the result:

If you are running old version of npm then its damn easy to update it to the latest version. Just use the following command from root:

Installing Modules using npm

There is a simple syntax to install any Node.js module:

For example, following is the command to install a famous Node.js web framework module called express:

Now you can use this module in your js file as following:

Global vs Local installation

By default, npm installs any dependency in the local mode. Here local mode refers to the package installation in node_modules directory lying in the folder where Node application is present. Locally deployed packages are accessible via require() method. For example when we installed express module, it created node_modules directory in the current directory where it installed express module.

Alternatively you can use npm ls command to list down all the locally installed modules.

Globally installed packages/dependencies are stored in system directory. Such dependencies can be used in CLI (Command Line Interface) function of any node.js but can not be imported using require() in Node application directly. Now Let’s try installing express module using global installation.

This will produce similar result but module will be installed globally. Here first line tells about the module version and its location where it is getting installed.

You can use following command to check all the modules installed globally:

Using package.json

package.json is present in the root directory of any Node application/module and is used to define the properties of a package. Let’s open package.json of express package present in node_modules/express/

Attributes of Package.json

  • name – name of the package
  • version – version of the package
  • description – description of the package
  • homepage – homepage of the package
  • author – author of the package
  • contributors – name of the contributors to the package
  • dependencies – list of dependencies. npm automatically installs all the dependencies mentioned here in the node_module folder of the package.
  • repository – repository type and url of the package
  • main – entry point of the package
  • keywords – keywords

Uninstalling a module

Use following command to uninstall a Node.js module.

Once npm uninstall the package, you can verify by looking at the content of /node_modules/ directory or type the following command:

Updating a module

Update package.json and change the version of the dependency which to be updated and run the following command.

Search a module

Search package name using npm.

Create a module

Creation of module requires package.json to be generated. Let’s generate package.json using npm, which will generate basic skeleton of the package.json.

You will need to provide all the required information about your module. YOu can take help from the above mentioned package.json file to understand the meanings of various information demanded. Once package.json is generated. Use the following command to register yourself with npm repository site using a valid email address.

Now its time to publish your module:

If everything is fine with your module, then it will be published in the reporsitory and will be accessible to install using npm like any other other Node.js module.

What is Callback?

Callback is an asynchronous equivalent for a function. A callback function is called at the completion of a given task. Node makes heavy use of callbacks. All APIs of Node are written is such a way that they supports callbacks.

For example, a function to read a file may start reading file and return the control to execution environment immidiately so that next instruction can be executed. Once file I/O is complete, it will call the callback function while passing the callback function, the content of the file as parameter. So there is no blocking or wait for File I/O. This makes Node.js highly scalable, as it can process high number of request without waiting for any function to return result.

Blocking Code Example

Create a text file named input.txt having following content

Create a js file named main.js which has the following code:

Now run the main.js to see the result:

Verify the Output

Non-Blocking Code Example

Create a text file named input.txt having following content

Update main.js file to have following code:

Now run the main.js to see the result:

Verify the Output

These two examples explain the concept of blocking and non-blocking calls. First example shows that program blocks until it reads the file and then only it proceeds to end the program where as in second example, program does not wait for file reading but it just proceeded to print “Program Ended” and same time program without blocking continues reading the file.

Thus, a blocking program executes very much in sequence and from programming point of view its easier to implement the logic but non-blocking programs does not execute in sequence, so in case a program needs to use any data to be processed, it should be kept with-in the same block to make it sequential execution.

Node js is a single threaded application but it support concurrency via concept of event and callbacks. As every API of Node js are asynchronous and being a single thread, it uses async function calls to maintain the concurrency. Node uses observer pattern. Node thread keeps an event loop and whenever any task get completed, it fires the corresponding event which signals the event listener function to get executed.

Event Driven Programming

Node.js uses events heavily and it is also one of the reasons why Node.js is pretty fast compared to other similar technologies. As soon as Node starts its server, it simply initiates its variables, delcares functions and then simply waits for event to occur.

In an event-driven application, there is generally a main loop that listens for events, and then triggers a callback function when one of those events is detected.

Event Loop

While Events seems similar to what callbacks are. The difference lies in the fact that callback functions are called when an asynchronous function returns its result where as event handling works on the observer pattern. The functions which listens to events acts as Observers. Whenever an event gets fired, its listener function starts executing. Node.js has multiple in-built events available through events module and EventEmitter class which is used to bind events and event listeners as follows:

Following is the syntax to bind event handler with an event:

We can fire an event programatically as follows:

Example

Create a js file named main.js having the following code:

Now let’s try to run the above program as check the output:

This will produce following result:

How Node Applications Work?

In Node Application, any async function accepts a callback as a last parameter and the callback function accepts error as a first parameter. Let’s revisit the previous example again. Create a text file named input.txt having following content

Create a js file named main.js having the following code:

Here fs.readFile() is a async function whose purpose is to read a file. If an error occurs during read of file, then err object will contain the corresponding error else data will contain the contents of the file. readFile passes err and data to callback function after file read operation is complete, which finally prints the content.

Many objects in Node emit events for example a net.Server emits an event each time a peer connects to it, a fs.readStream emits an event when the file is opened. All objects which emit events are instances of events.EventEmitter.

EventEmitter Class

As we have seen in previous section, EventEmitter class lies in events module. It is accessibly via following syntax:

When an EventEmitter instance faces any error, it emits an ‘error’ event. When new listener is added, ‘newListener’ event is fired and when a listener is removed, ‘removeListener’ event is fired.

EventEmitter provides multiple properties like on and emit. on property is used to bind a function with the event and emit is used to fire an event.

Methods

S.N. method & Description
1 addListener(event, listener)
Adds a listener to the end of the listeners array for the specified event. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of event and listener will result in the listener being added multiple times. Returns emitter, so calls can be chained.
2 on(event, listener)
Adds a listener to the end of the listeners array for the specified event. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of event and listener will result in the listener being added multiple times. Returns emitter, so calls can be chained.
3 once(event, listener)
Adds a one time listener for the event. This listener is invoked only the next time the event is fired, after which it is removed. Returns emitter, so calls can be chained.
4 removeListener(event, listener)
Remove a listener from the listener array for the specified event. Caution: changes array indices in the listener array behind the listener. removeListener will remove, at most, one instance of a listener from the listener array. If any single listener has been added multiple times to the listener array for the specified event, then removeListener must be called multiple times to remove each instance. Returns emitter, so calls can be chained.
5 removeAllListeners([event])
Removes all listeners, or those of the specified event. It’s not a good idea to remove listeners that were added elsewhere in the code, especially when it’s on an emitter that you didn’t create (e.g. sockets or file streams). Returns emitter, so calls can be chained.
6 setMaxListeners(n)
By default EventEmitters will print a warning if more than 10 listeners are added for a particular event. This is a useful default which helps finding memory leaks. Obviously not all Emitters should be limited to 10. This function allows that to be increased. Set to zero for unlimited.
7 listeners(event)
Returns an array of listeners for the specified event.
8 emit(event, [arg1], [arg2], […])
Execute each of the listeners in order with the supplied arguments. Returns true if event had listeners, false otherwise.

Class Methods

S.N. method & Description
1 listenerCount(emitter, event)
Return the number of listeners for a given event.

Events

S.No. Events & Description
1 newListener

  • event – String The event name
  • listener – Function The event handler function

This event is emitted any time a listener is added. When this event is triggered, the listener may not yet have been added to the array of listeners for the event.

2 removeListener

  • event – String The event name
  • listener – Function The event handler function

This event is emitted any time someone removes a listener. When this event is triggered, the listener may not yet have been removed from the array of listeners for the event.

Example

Create a js file named main.js with the following Node.js code:

Now run the main.js to see the result:

Verify the Output

Pure JavaScript is Unicode friendly but not nice to binary data. When dealing with TCP streams or the file system, it’s necessary to handle octet streams. Node provides Buffer class which provides instances to store raw data similar to an array of integers but corresponds to a raw memory allocation outside the V8 heap.

Buffer class is a global class and can be accessed in application without importing buffer module.

Creating Buffers

Node Buffer can be constructed in a variety of ways.

Method 1

Following is the syntax to create an uninitiated Buffer of 10 octets:

Method 2

Following is the syntax to create a Buffer from a given array:

Method 3

Following is the syntax to create a Buffer from a given string and optionally encoding type:

Though “utf8” is the default encoding but you can use either of the encodings “ascii”, “utf8”, “utf16le”, “ucs2”, “base64” or “hex”.

Writing to Buffers

Syntax

Following is the syntax of the method to write into a Node Buffer:

Parameters

Here is the description of the parameters used:

  • string – This is string data to be written to buffer.
  • offset – This is the index of the buffer to start writing at. Default value is 0.
  • length – This is the number of bytes to write. Defaults to buffer.length
  • encoding – Encoding to use. ‘utf8’ is the default encoding

Return Value

This method returns number of octets written. If there is not enough space in the buffer to fit the entire string, it will write a part of the string.

Example

When above program is executed, it produces following result:

Reading from Buffers

Syntax

Following is the syntax of the method to read data from a Node Buffer:

Parameters

Here is the description of the parameters used:

  • encoding – Encoding to use. ‘utf8’ is the default encoding
  • start – Beginning index to start reading, defaults to 0.
  • end – End index to end reading, defaults is complete buffer.

Return Value

This method decodes and returns a string from buffer data encoded using the specified character set encoding.

Example

When above program is executed, it produces following result:

Convert Buffer to JSON

Syntax

Following is the syntax of the method to convert a Node Buffer into JSON object:

Return Value

This method returns a JSON-representation of the Buffer instance.

Example

When above program is executed, it produces following result:

Concatenate Buffers

Syntax

Following is the syntax of the method to concatenate Node buffers to a single Node Buffer:

Parameters

Here is the description of the parameters used:

  • list – Array List of Buffer objects to be concatenated
  • totalLength – This is the total length of the buffers when concatenated

Return Value

This method returns a Buffer instance.

Example

When above program is executed, it produces following result:

Compare Buffers

Syntax

Following is the syntax of the method to compare two Node buffers:

Parameters

Here is the description of the parameters used:

  • otherBuffer – This is the other buffer which will be compared with buf

Return Value

Returns a number indicating whether this comes before or after or is the same as the otherBuffer in sort order.

Example

When above program is executed, it produces following result:

Copy Buffer

Syntax

Following is the syntax of the method to copy a node buffer:

Parameters

Here is the description of the parameters used:

  • targetBuffer – Buffer object where buffer will be copied.
  • targetStart – Number, Optional, Default: 0
  • sourceStart – Number, Optional, Default: 0
  • sourceEnd – Number, Optional, Default: buffer.length

Return Value

No return value. Copies data from a region of this buffer to a region in the target buffer even if the target memory region overlaps with the source. If undefined the targetStart and sourceStart parameters default to 0 while sourceEnd defaults to buffer.length.

Example

When above program is executed, it produces following result:

Slice Buffer

Syntax

Following is the syntax of the method to get a sub-buffer of a node buffer:

Parameters

Here is the description of the parameters used:

  • start – Number, Optional, Default: 0
  • end – Number, Optional, Default: buffer.length

Return Value

Returns a new buffer which references the same memory as the old, but offset and cropped by the start (defaults to 0) and end (defaults to buffer.length) indexes. Negative indexes start from the end of the buffer.

Example

When above program is executed, it produces following result:

Buffer Length

Syntax

Following is the syntax of the method to get a size of a node buffer in bytes:

Return Value

Returns a size of buffer in bytes.

Example