Node Js : Intro and Setup

Node.js is a server side implementation of JavaScript built on top of Google’s V8 Javascript engine. It’s a highly scalable system that uses asynchronous, event-driven I/O (input/output), rather than threads or separate processes. It’s ideal for web applications that are frequently accessed but computationally simple.

What Problem does node solve?

If you’re using a traditional web server, such as Apache, each time a web resource is requested, Apache creates a separate thread or invokes a new process in order to handle the request. Even though Apache responds quickly to requests, and cleans up after the request has been satisfied, this approach can still tie up a lot of resources. A popular web application is going to have serious performance issues. As your client-base grew, if you wanted your web application to support more users, you had to add more and more servers. Of course, this adds to a business’s server costs, traffic costs, labour costs, and more.
Node solves this issue by changing how a connection is made to the server. Instead of spawning a new OS thread for each connection (and allocating the accompanying memory with it), each connection fires an event run within the Node engine’s process. Node also claims that it will never deadlock, since there are no locks allowed, and it doesn’t directly block for I/O calls. Node claims that a server running it can support tens of thousands of concurrent connections.

Node is designed to be used for applications that are heavy on input/output (I/O), but light on computation. More importantly, it provides this functionality directly out of the box. You don’t have to worry about the application blocking any further processing while waiting for a file to finish loading or a database to finish updating, because most of the functionality is asynchronous I/O by default. And you don’t have to worry about working with threads, because Node is implemented on a single thread.

The server-side is actually no different than the client-side. True, there are no buttons getting pressed, and no text fields getting typed in, but on a higher level, events are taking place. A connection is made -event! Data is received through the connection -event! Data stops coming through the connection – event.

Why is this type of setup ideal for Node? JavaScript is a great language for event-driven programming, because it allows anonymous functions and closures, and more importantly, the syntax is familiar to nearly everyone who has ever coded. The callback functions that are called when an event occurs can be written in the same spot where you capture the event. Easy to code, easy to maintain. No complicated Object Oriented frameworks, no interfaces, no potential for over-architecting anything. Just listen for an event, write a callback function, and everything is taken care of.

Installing Node

You can download node from its official website accouring to your OS. – Download

I am using windows , and downloaded the Windows Installer (.msi) 64 bit one. The installations is pretty straight forward. After the installation is complete, open command promt and type
>node –version

This should print the version of node that you are using. Lets create a file called helloWorld.js with following line:

console.log(“Hello World”);

Lets run it


Although it looks straight forward, but there is lots of stuff going on the backend. The v8 js engine actually doesnt know the concept of I/O. It can run on many devices lie browser,server etc. Every one has a different concept of I/O. Here node is providing a hosting environment for javascript, same as the browser provides a hosting environment for javascript. This environment is what allows us to do stuff with it, like printing on the screen.

Lets take an argument from the command line

var input=process.argv[2];

console.log("Hello "+input);

Above we used the 2 because first 2 are node and the file name. Running it:



Node.js ships with a REPL, which is short for ‘Read-Eval-Print Loop’. It’s essentially an interactive prompt that allows you to do pretty much anything that you can do with regular Node, but without all the overhead of creating a separate file, and it’s great for experimentation.

Running it is simple – just run node without a filename.


REPL then provides a command-line prompt—an angle bracket (>)—by default. Anything you type from this point on is processed by the underlying V8 JavaScript engine.As in most shells, you can press the up and down arrow keys to scroll through your command history and modify previous commands. The REPL also Tab to make the REPL try to autocomplete the command. Whenever you type a command, it will print the return value of the command. If you want to reuse the previous return value, you can use the special ‘_’ variable.


As you can see when declaring with var it doesn’t return the value 4, it returns a value of undefined. The reason is that the result of the expression is undefined, since variable assignment doesn’t return a result when evaluated.

The benefit of REPL is to get a closure understanding of Javascript.


Similarly you can write function:


REPL commands:
.clear //Resets the context object and clears any multiline expression. This command basically starts you over again.
.exit //Exits REPL.
.help //Displays all available REPL commands.
.save //Saves the current REPL session to a file.
.load //Loads a file into the current session (.load /path/to/file.js).
.break //If you get lost during a multiline entry, typing .break will start you over again. You’ll lose the multiline content, though.

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

%d bloggers like this: