node.js in Action : Let’s Start With node.js

This article is an introduction to node.js and describes it in action.

We can say that node.js is one of the popular buzzwords in the modern software industry. Most probably, you have heard the term but it does not matter if you have not because I have launched this series to talk about node.js and it’s real use in applications.

Since node.js is a JavaScript platform, it is necessary to have prior knowledge of JavaScript and the concepts and functionality of web servers will help you to understand the nutshell of the request and response process provided by node.js.

So, in summary, at the day’s end node.js is nothing but a piece of JavaScript code that acts as web server software (yes, almost a full-fledged web server if we configure it) and to implement our own web server using node.js. So let’s start with a small discussion and example of node.js.

What node.js is

In simple terms, “JavaScript on a server”. I remember my college days where I had been taking a few classes on the topic of “JavaScript” (on behalf of one of my favorite lazy teachers, who sat in the last bench when I was with the blackboard and ppt, haha..)

I often said that JavaScript runs in the client, it always runs in the client browser , the browser knows how to parse and execute JavaScript code bla bla..

My dear friends (those students who were in class on those days) I was not 100% correct. JavaScript can run in a server. Moreover we can build a server using JavaScript. Yes, this is the real power of JavaScript in web programming.



First of all let’s salute to the creator of node.js, Ryan Dhal. Who invented node.js in 1990. It’s development and maintenance is sponsored by Joyent. Basically Ryan Dhal was inspired to create node.js by seeing a file uploading system in flicker.com . Dhal wanted to made it easier with his own innovative idea. He started to develop something in C, Lua and Haskell but the project failed in those platforms but was released in JavaScript by the power of the V8 engine (that gives power for the Chrome browser). He has used the beautiful concept of a non- blocking I/O mechanism of the JavaScript programming language and in my view this is the real power of node.js. It provides endless scalability of an application with the magic of non-blocking I/O.

We now understand that node.js is a platform to build a HTTP server using the JavaScript programming language that can handle a huge amount of HTTP requests and the node.js platform runs on top of Google’s V8 JavaScript engine that gives power to the Chrome browser too.

Fine, then a question may come to mind, “What about IIS, Apache and some other popular web server”? Will those be obsolete in the near future? My answer is no. Accepting that node.js is cool, easy and is able to handle a huge request but still it has its own limitation. node.js is not a Silver bullet in the software industry that can replace an existing web server overnight (each technology has its own space and power to survive, haha..)

Which kind of application is fit for node.js?

This is very important to discuss, it’s not like we know that node.js will be used in our next project. node.js is a “Data Intensive Real-Time” (DIRT) type of application. The most popular example is Facebook. Or the application that is very much data centric and very interactive in nature. Because the fundamental idea of a node server is scalability.

Let’s implement our first node.js server

It is our first initiation of a up and running node.js application. If you are experienced with node.js server creation then you know how easy it is and for the novice, it will take a maximum of 10 minutes to run our first node.js.

Step 1: Download the node.js installer file from here.

I am using the Windows OS so I chose the Windows version, you may choose a different version depending on your underlying operating system.

Step 2: Install the .msi file (for windows) in the system.

Once you have installed node.js in the system, you should get the node.js start option in the Start menu.



If you get it then “All is well” and we are ready to run a node.js server in our system.

Step 3: Implement the simple server using JavaScript.

In this step we will implement our node.js server. Yes, the code snippet below is enough to run our own HTTP server. Let’s write the code blindly and in the following we will explain it.

var http = require('http');

http.createServer(function (req, res) {

    res.writeHead(200, { 'Content-Type''text/plain' });

    res.end('Hey! Node.js is runnign.');

}).listen(8080, "127.0.0.1");

console.log('Server running at http://127.0.0.1:8080/');

Save your code somewhere in the system, I chose C:\node to save the JavaScript file and I have given as the filename “test.js”. You are free to choose any name.



Step 4: Run the server using a command prompt

In this step we will bring up our web server that we created just now. Open a command prompt in “Administrator mode”. Please keep in mind the Administrator mode is necessary to run the server.

Then go to your file location and bring up the server using the following command:

node <filename>.js



We see that the server is up and running in port 8080 in the local host. It’s unnecessary to say that you are free to choose your own port (but please don’t mess with a well known port).

Fine, the server is running so that we consume the service using our web client. Follow the next step to consume the service from the server.

Step 5: Make HTTP request to the node.js server.

This is the final step and we are very near to our result.



Oh, Cheers! We got a HTTP response from our node.js server we created just now.

Good, we are getting output; now let’s discuss the tiny server code. As we have said, node.js is the platform for building a HTTP server using the JavaScript language and in reality we are using the following JavaScript code snippet to bring up and run our HTTP server.

var http = require('http');

http.createServer(function (req, res) {

    res.writeHead(200, { 'Content-Type''text/plain' });

    res.end('Hey! Node.js is runnign.');

}).listen(8080, "127.0.0.1");

console.log('Server running at http://127.0.0.1:8080/');

The first line loads the “http” module in the application that we will discuss deeply in a future article. The following lines are the heart of the web server.

http.createServer(function (req, res) {

    res.writeHead(200, { 'Content-Type''text/plain' });

    res.end('Hey! Node.js is runnign.');

}).listen(8080, "127.0.0.1");

We are calling the createServer() method of the http module by passing a callback function into it. The callback function is nothing but an endpoint where we can receive and send a HTTP response and request. Within the function we are setting the Content-type as “text/plain” with status code 200 that means OK and in the following line we are making a HTTP response by sending plain text from the server.

res.end('Hey! Node.js is runnign.');

Then we are binding the server in the local host with port 8080.

Pretty cool and simple. Is not it?

Let’s check the response header of the HTTP response of our node.js server using the Fiddler tool.



We are seeing that the response type is text/plain since we have set in our server configuration and the status code is 200 (OK response).

Conclusion

This is the introductory article of the node.js in Action article series. Here we learned how to implement a simple node server using a few lines of JavaScript code. But the actual full-blown node server is more complex than that. In a future article we will learn a few more interesting concepts of node.js. Have a happy day. See you in the next article.