Introduction To NodeJS

So we have been hearing a lot about node.js and its popularlarity among web developers and IOT specialists, but what exactly is it and why is it widely used as a primary language for development? Let us 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 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.

  1. //example 1  
  2. var result = database.query("SELECT * FROM sampletable");  
  3.   
  4. console.log("finished");  
  5.   
  6. console.log("start");  
  7.   
  8. //example 2  
  9.   
  10. database.query("SELECT * FROM sampletable", function(result) {  
  11.   
  12. console.log("finished");  
  13.   
  14. });  
  15.   
  16. console.log("start");  
output

Would be:

finished
start
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 won't.

Installation

First you need a Window OS that is at least Windows Vista, then download the MSI. 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.
  1. var http = require("http");  
  2.   
  3. var server = http.createServer(function(request, response) {  
  4.   
  5. response.writeHead(200, {"Content-Type""text/html"});  
  6.   
  7. response.write("Hello World");   
  8.   
  9. response.end();  
  10.   
  11. });  
  12.   
  13. server.listen(8080);  
  14.   
  15. 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.
 
Read more articles on Node.js:

Up Next
    Ebook Download
    View all
    Learn
    View all