Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / IoT

Introduction to Node.Js

2.00/5 (6 votes)
17 May 2016CPOL3 min read 11.2K  
Let us understand the difference between asynchronous and synchronous programming in simple terms which will give us an understanding how node.js work behind the scene and one of the reason for its popularity.

This article is for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers

Get access to the new Intel® IoT Developer Kit, a complete hardware and software solution that allows developers to create exciting new solutions with the Intel® Galileo and Intel® Edison boards. Visit the Intel® Developer Zone for IoT.

So we have been hearing a lot about node.js and its popular among web developers and IOT specialist but what exactly it is and why is it widely used as primary language for development. Let go through this article to understand it.

According to nodejs.org, "node.js is a platform built on Chrome's JavaScript Runtime". In other words the code you write for the server will execute in a very similar manner in the browser. You can easily carry the JavaScript techniques you have learned elsewhere. It is fast, event-driven and lightweight but the biggest advantage of using the node.js is the asynchronous programming.

Let us understand the difference between asynchronous and synchronous programming in simple terms which will give us an understanding how node.js work behind the scene and one of the reason for its popularity.

Synchronous

You are in a queue to get a movie ticket. You cannot get one until everybody in front of you gets one, and the same applies to the people queued behind you.

Asynchronous

You are in a restaurant with many other people. You order your food. Other people can also order their food, they don't have to wait for your food to be cooked and served to you before they can order. In the kitchen restaurant workers are continuously cooking, serving, and taking orders. People will get their food served as soon as it is cooked.

JavaScript
//example 1
var result = database.query("SELECT * FROM sampletable");
console.log("finished");
console.log("start");
//example 2
database.query("SELECT * FROM sampletable", function(result) {
    console.log("finished");
});
console.log("start");

output

Would be:

  1. finished
    start
  2. start
    finished

The difference is that in the first example, the program will block in the first line. The next line (console.log) will have to wait. In the second example, the console.log will be executed while the query is being processed. That is, the query will be processed in the background, while your program is doing other things, and once the query data is ready, you will do whatever you want with it.

So, in a nutshell : The first example will block, while the second wont.

Installation

First you need a Window OS that is at least Windows Vista, then download the MSI from http://nodejs.org/download/. Make sure you select the add to path option while installing node.js on your windows machine.

Creating your own node.js server

Start by creating a new file named "server.js". Insert the following code into the file and save it.

JavaScript
var http = require("http");
var server = http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/html"});
  response.write("Hello World");                    
  response.end();
});
server.listen(8080);
console.log("Server is listening at port 8080");

To start the server, type the command shown below. If everything works properly, you will see a message that the server is listening.

node server.js

The next step is to connect to the server using a web browser. Launch your browser of choice, and direct it to either of the following links. In networking terms, localhost (and it’s IP address of 127.0.0.1) refers to the machine you are currently using. Your browser should be saying "Hello World".

http://localhost:8080

How the server works

The require(): Node.js provides a simple module system. Node.js programs can load individual modules using the require() method. While many modules must be explicitly downloaded, some modules, such as http called core modules are bundled with node.js.

The HTTP server is created using the http module’s createServer() method. The createServer() takes a callback function as an argument. This callback function is executed each time the server receives a new request.

The callback function takes two arguments, request and response. The request object contains information regarding the client’s request, such as the URL, HTTP headers, and much more. Similarly, the response object is used to return data back to the client.

The call to listen()causes the server to bind to a port and listen for incoming connections. here we have defined port to be 8080.

In the next article we will discuss more on how node.js work with event loop and in details about node modules and how to connect to mongoDB, a NoSQL database.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)