Node.js Tutorial : How To Install and Create an Application

what is node.js

Nodejs.org states node.js as

“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.”

Node is a platform build on Google V8 java script engine with the goal to write network applications well suited for DIRT(data intensive real-time) applications. Its distinguished feature is that you can write code in asynchronous non blocking style with the benefit of being super scalable. Now lets understand event-driven and non blocking I/O  models before we dive in to node.js.

What is event-driven ?

Event driven is a programming model where the program flow is based on events such as sensor outputs , or user events like mouse clicks , inputs from other messaging sources or threads etc. For more information on event-driven programming, you can read this blog post

What is Non Blocking I/O?

Non blocking operations does not block program executions. For instance, if you take node.js, it uses only one thread for all service requests. Every request arrives at the server will be serviced at a time. For example, when one request is processed to query the database, it will process the code to query the database and does not wait for the response, instead it sends a callback to the second queue and the code will continue running. When the database returns results, it will send  a callback to the third queue and when the engine is freed, it will execute the pending requests in the third queue.

Installing node.js on Windows

1. Head over to http://nodejs.org/download/ page and downland the appropriate msi or exe package (32 0r 64 bit).

2. Install the downloaded package like you install any other windows package.

3. verify the installation by executing the following command in command prompt or  powershell.

node --version
PS C:\Users\devopscube> node --version
v0.10.35

Installing node.js on Linux platform

Node.js can be installed on Linux systems by two ways.

  1. From source code
  2. From native repository

Installing from apt repository

1. execute the following curl command for setup.

curl -sL https://deb.nodesource.com/setup | sudo bash -

2. Install using apt-get

sudo apt-get install -y nodejs
[email protected]:~# sudo apt-get install -y nodejs 
.
. 
.update-alternatives: using /usr/bin/rlwrap to provide /usr/bin/readline-editor (readline-editor) in auto mode
Setting up nodejs (0.10.35-1nodesource1~trusty1) ...

3. verify the installation using the following the command.

node --version
[email protected]:~# node --version
v0.10.35

Installing from source

1. Download the source from github.

git clone https://github.com/joyent/node.git
[email protected]:~# git clone https://github.com/joyent/node.git
Cloning into 'node'...
remote: Counting objects: 135493, done.
remote: Compressing objects: 100% (24/24), done.
remote: Total 135493 (delta 1), reused 23 (delta 1)
Receiving objects: 100% (135493/135493), 112.28 MiB | 14.53 MiB/s, done.
Resolving deltas: 100% (101171/101171), done.
Checking connectivity... done.

2. Execute the  following command to install node.js.

cd node && ./configure && make && sudo make install

Note: You might get the following error while installing. If you do, install make and gcc using the command given below the error and execute the above command.

[email protected]:~# cd node && ./configure && make && sudo make install
Node.js configure error: No acceptable C compiler found!
        Please make sure you have a C compiler installed on your system and/or
        consider adjusting the CC environment variable if you installed
        it in a non-standard prefix.
The program 'make' is currently not installed. You can install it by typing:
apt-get install make

Install make and gcc by executing the following commands individually.

sudo apt-get install -y make 
sudo apt-get install -y gcc

Creating a simple web server application using node.js

In this section we will create a simple web server application running on port 80 for testing.

1. Create a file name server.js and copy the following contents on to the file.

var syslog = require("sys"),
my_server = require("http");
my_server.createServer(function(request,response){
    syslog.puts("I got a server request");
    response.writeHeader(200, {"Content-Type": "text/plain"});
    response.write("Hurray! You just tried a simple nodejs Application");
    response.end();
}).listen(80);
syslog.puts("Server Running on 80");

The above code runs a web server on port 80 displaying the message we specified in the code. Before executing the code let see what each line does in the above code.

var syslog = require("sys")

The above line loads the sys class in to syslog variable. We used this class output log in the console.

my_server = require("http");

It loads the http library to perform the web server tasks.

my_server.createServer(function(request,response){ 
.
.
}).listen(80);

This function accepts the requests on port 80 and displays the message  specified in the code. Also whenever a request is made, the syslog object displays a message on terminal.

Lets run our web server application by executing the following node command.

node server.js

Now you will be able to access the server on port 80. Open a new terminal and  make a request using curl  to local host on port 80 to check if our application is running.

curl http://localhost:80

you will get the message as output and the console will log the “I got a server request” message.

[email protected]:~# curl http://localhost:80
Hurray! You just tried a simple nodejs Application
[email protected]:~# node server.js
Server Running on 80
I got a server request

Now you have a running node.js application running on your system. There are many frameworks available for node.js web application development. Express.js is a matured node.js framework for application development. You can start with express.js if you are going to create an application using node.js.

Tell us what you think about this post in the comments section.

Free DevOps Resources

Get DevOps news, tutorials and resources in your inbox. A perfect way If you want to get started with devops. Like you, we dont like spam.

1 Comment

  1. Alain van Hoof Reply

    On a Mac (running OS X) one is always logged in as a user and not as root. To run the server either use:
    “sudo node server.js” or change the listening port to a non privileged number ( >1024) to 8080 for example and use the command “curl http://localhost:8080

Leave a Reply

Your email address will not be published. Required fields are marked *