Node JS – Part:I

Node JS  – Part – I:

Node.js is an event based, asynchronous I/O framework.

Node.js – or just Node as it’s commonly called – is used for developing applications that make heavy use of the ability to run JavaScript both on the client, as well as on server side and therefore benefit from the re-usability of code and the lack of context switching.

How Traditional Web server is working:

  1. So here’s how it is, how it’s always been: a browser sends a request to a website. The site’s server receives the request, tracks down the requested file, performs any database queries as needed, and sends a response to the browser. In traditional web servers, such as Apache, each request causes the server to create a new system process to handle that request.

Ex: apache server

Ajax Call:

  1. Then there was Ajax. Instead of requesting a whole new page each time, we’d only request the piece of information we actually wanted. Okay, that’s an improvement.

Ex: don’t want send entire page èto server è send only piece of info

Why we want to use NodeJS:

For example, as soon as one of your friends adds a new post, the server returns the response telling the browser to update its display. As soon as the browser receives the response, it shoots back another request. This way the browser is always waiting for a new event to happen on the server side.

Now think about what that means for a traditional web server like Apache. For each and every user connected to the site, your server has to keep a connection open. Each connection requires a process, and each of those processes will spend most of its time either sitting idle (consuming memory) or waiting on a database query to complete. This means that it’s hard to scale up to high numbers of connections without grinding to a near halt and using up all your resources.

Resolve above problems: Node js

So what’s the solution? Here’s where some of that jargon from before comes into play: specifically non-blocking and event-driven. What those terms mean in this context is less complicated than you might fear. Think of a non-blocking server as a loop: it just keeps going round and round. A request comes in, the loop grabs it, passes it along to some other process (like a database query), sets up a callback, and keeps going round, ready for the next request. It doesn’t just sit there, waiting for the database to come back with the requested info.

If you want run Nodejs in your machine, follow below steps. You will get some ideas about Nodejs

 How to Install Nodejs:

  1. Download Node files from If you need source code click “Source Code” link. Otherwise download exe file from “Windows Installer” link. My suggestion is better click “Windows Installer” link.

  2. Once you click “Windows Installer” then you will get exe files called “node-v0.8.4-x86.msi”.

  3. Download and installed in your system. Once you installed, nodejs related files will locate in to your “C:\Program Files\nodejs” (as per machine installation path, it may change up to your path location).

  4. Installation is done.

How you want to run node js file(s):

  1. Using cmd prompt

  2. (OR) using node cmd prompt (start->nodejs->nodejs command prompt)

  3. You can see the output browser and also command prompt.

Where you want to keep the files (run a nodejs file in two ways):

  1. Way -> 1, Keep Nodejs related files in “C:\Program Files\nodejs” path and run your examples. (This is default once you install node.exe then this path automatically will take)

  2. Way -> 2, Another way is we will keep it in other than C: Drive also. After node installation is done, then copy “nodejs” folder from “ program files “  and keep it in your wamp (in mycase I keep it in inside wamp folder, you can place wherever want).

Ex: D:\wamp\www\MYPATH\NodeJS_Working\nodejs (This is my path, it may change in your machine)

Way: I -> keep your files from C: Drive:

  1. Run “helloworld.js” file

  2. Example programs are given below these steps. Please take a copy and run it.

  3. Keep you “helloworld.js” inside “C:\Program Files\nodejs\ helloworld.js”

  4. Open “Cmd prompt (or) nodejs” command prompt.

  5. Give below path in cmd prompt

C:\Program Files\nodejs>node helloworld.js

  1. Once you enter with this command, then the command is starting to listen you port (ie) you file is ready

  2. Open browser and give below path

 http://localhost:8888   (8888 is a listener port. Check your helloworld.js file.)

Output: Hello World

  1. If you give other than port: 8888 then your “helloworld.js” won’t work. What port you used in your file, the same port number you have to use in your browser.

  2. Refer below screenshot – 1.

 Screenshot – 1


10.   Code for helloworld.js:

 var http = require(“http”);

http.createServer(function(request, response) {

  response.writeHead(200, {“Content-Type”: “text/plain”});

  response.write(“Hello World”);



Note: 8888 is a listener port. What port you use here, then same port you have to use it in your browser, while running this file

 Code Explanation:

1. The first line requires the http module that ships with Node.js and makes it accessible through the variable http.

2. We then call one of the functions the http module offers: createServer. This function returns an object, and this object has a method named listen, and takes a numeric value which indicates the port number our HTTP server is going to listen on.

3. Please ignore for a second the function definition that follows the opening bracket of http.createServer.

We could have written the code that starts our server and makes it listen at port 8888 like this:

 var http = require(“http”);

var server = http.createServer();


 4. That would start an HTTP server listening at port 8888 and doing nothing else (not even answering any incoming requests).

5. The really interesting (and, if your background is a more conservative language like PHP, odd looking) part is the function definition right there where you would expect the first parameter of the createServer() call.

6. Turns out, this function definition IS the first (and only) parameter we are giving to the createServer() call. Because in JavaScript, functions can be passed around like any other value.

Way: II -> Keep your files Other than C: Drive:

In my case I keep nodejs files in inside D:\wamp\www\MYPATH folder. You can keep wherever you want. But don’t forget, we should use proper path in command prompt while running a file.

  1. My file path is


  1. Open “Cmd prompt (or) nodejs” command prompt.

  2. Give below path in cmd prompt

D:\wamp\www\MYPATH>node helloworld.js

  1. Open browser and give -> http://localhost:8888/

  2. Check sample screenshot- 2

Screenshot – 2