Click here to Skip to main content
14,096,101 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

7.1K views
4 bookmarked
Posted 17 May 2016
Licenced CPOL

Introduction to Node.Js

, 17 May 2016
Rate this:
Please Sign up or sign in to vote.
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.

//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.

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)

Share

About the Author

Intel Corporation
United States United States
You may know us for our processors. But we do so much more. Intel invents at the boundaries of technology to make amazing experiences possible for business and society, and for every person on Earth.

Harnessing the capability of the cloud, the ubiquity of the Internet of Things, the latest advances in memory and programmable solutions, and the promise of always-on 5G connectivity, Intel is disrupting industries and solving global challenges. Leading on policy, diversity, inclusion, education and sustainability, we create value for our stockholders, customers and society.
Group type: Organisation

43 members


You may also be interested in...

Comments and Discussions

 
QuestionHuh? Pin
Richard Algeni Jr18-May-16 11:48
memberRichard Algeni Jr18-May-16 11:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190518.1 | Last Updated 17 May 2016
Article Copyright 2016 by Intel Corporation
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid