A Brief Introduction to Node.JS

2 min read

Nodejs And Other JavaScript

A beginner like myself would have first come across JavaScript when developing HTML Documents were we used JavaScript in a very basic way, adding interactivity to our web pages. We then started to build complex web sites using Ruby, PHP,Java etc., by writing backend code. And with the introduction of jQuery and the likes of Prototype, things became advanced with JavaScript. But all of this are frontend stuff and we are merely users of JavaScript rather than JavaScript developers. Nodejs on the contrary brings JavaScript to the server-side.

Nodejs

Nodejs is JavaScript on the server-side, which allows us to run JavaScript code in the backend. It makes use of Google’s V8 VM run-time environment to interpret and execute JavaScript on the backend. This is the same JavaScript execution engine built for Google Chrome. Nodejs also provides some useful modules, to avoid writing things from scratch. So Nodejs is nothing but a run-time environment with a set of libraries.

How Nodejs does its magic ?

Nodejs uses event-driven, non-blocking I/O to allow data-intensive real-time applications to run across distributed services and still remain lightweight and efficient. Compared to traditional web-serving techniques where each request spawns a new thread, taking up system RAM and eventually maxing-out at the amount of RAM available, Node.js operates on a single-thread, using non-blocking I/O calls, allowing it to support tens of thousands of concurrent connections held in the event loop.
toptal-blog-1_B
Image copyright: Node.js core team.
This ofcourse leads to the question of what happens when a single thread is shared between all client requests , well Nodejs handles this potential pitfall using Asynchronous callbacks and event loops which we shall see next.

Event Loop

Node.js registers itself with the operating system, so that it is notified when a connection is made. When a connection is made, the operating system will issue a callback. Within the Node.js runtime, each connection is a small heap allocation. Node.js, uses an event loop, instead of processes or threads, to scale to millions of connections happening at the same time. In contrast to other event-driven servers, Node.js’ event loop does not need to be called explicitly. Instead callbacks are defined, and the server automatically enters the event loop at the end of the callback definition and exits the event loop when there are no further callbacks to be performed.

Writing Hello World! in Node.js

1. Open a file in your favorite editor and type the following :
% console.log("Hello World !");
2.save the file as helloworld.js and execute it through Node.js in the terminal :
% node helloworld.js
This should output- Hello World !- on your terminal. But this is basic stuff, so lets write some real stuff !

A Basic HTTP Server

1. open a new file and name it as server.js with the following content :
[source language=”JavaScript”]
var http = require(“http”);
http.createServer(function(request, response) {
response.writeHead(200, {“Content-Type”: “text/plain”});
response.write(“Hello World”);
response.end();
}).listen(8888);
[/source]
So, this should give us a working http server , which we can start by executing the the script with Node.js :
% node server.js
Now if we open our browser and hit the url – http://localhost:8888 , the web-page should display “Hello World”.

Analyzing our code

We first require one of the modules that Node.js provides us which is the “http” module and assign it to a variable. Usually it’s considered good practice to set the name of this variable to be the same as the name of the module that we require.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.
Next we see what the anonymous function does : It has two parameters ‘request’ and ‘response’ which are objects of our http module. We can use their methods to handle the details of the HTTP request that occured and to respond to the request. Whenever a request is received, it uses the response.writeHead() function to send an HTTP status 200 and content-type in the HTTP response header, and the response.write() function to send the text “Hello World” in the HTTP response body. At last, we call response.end() to actually finish our response.
I am hoping to share more advanced stuff in Node.JS soon.

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *