Node JS – Introduction

Node JS is a framework which whom is possible to generate JS server side applications. It take advantage from the V8 Google’s engine, which compiles to native machine code and dynamically optimizes it at run time by using heuristic on the execution profile. Even though is very diffused in the Unix systems is becoming used in the Windows platform as well, since Microsoft has strongly invested for its porting and its support.

The first thing to do is to download Node and to install it. It will be installed also NPM, the packet manager which whom we’ll be able to add other features to the base framework.

First Application

In order to create a Node JS application we have to create a JS file and to execute it with Node. Within the file (that is the entry point for the server side application) we insert the server configuration and the various functions that we’ll use to manage the routes. Here is an example:

var http = require("http");
http.createServer(function (request, response) {
    response.writeHead(200, { "Content-Type": "text/plain" });
    response.write("Hello World");
console.log("server started");

To launch the application we have to start the console (running as administrator) and to execute Node JS with our file as parameter, in this way:

We can see the message “server started” printed by the application. If we try to connect to http://localhost:8888 (the port in which we set out application to listen) we’ll see the message “hello world” that we are returning to the from server side. To end the application we can press on CTRL + C.

Node JS application with Express

Express is an additional framework very useful to create Node JS applications in a way that is more similar to when we work with a “classic” web server. It can be installed from NPM (we can use the npm search express command to find it), but before we need to create a file named package.json, which describes our application and, most important, our dependencies. When we launch the npm install command upon our site’s root (that should be the correct location for this file) the file is read and the dependencies are automatically downloaded and installed. In our case, since we want to use Express, the package.json file will be similar to this example:

    "name": "hello-world",
    "version": "0.0.1",
    "dependencies": {
        "express": "latest"

After correctly installed Express we can create our application and to launch it in a similar way as we have done previously. Here is an example of an application which uses Node JS with Express:

var express = require('express');
var app = express();
app.get('/', function (req, res) {
    res.send('This is the home page');
app.get('/about', function (req, res) {
    res.send('This is the about page');

If we try to connect to http://localhost:3000 we will see the message “this is the home page”. If we try to connect to “http://localhost:3000/about we will see the message “this is the about page”.


One thought on “Node JS – Introduction

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s