What is nodejs? What are blocking I/O operations ? What is event driven programing

Nodejs is a server side javascript runtime environment built on top of google chrome's v8 engine. With the help of node.js you can write server side business logic in JavaScript making full stack development easy as you will be using JavaScript on both front end and Backend (Browser apps).

Node.js uses event driven, non-blocking I/O which helps in writing rich, scalable webapps with high performance. Node.js's package manager(npm) is a largest ecosystem of open source libraries, helping in quick development, for example express.js, socket.io and many thousands of more open source packages are available.

Don't worry if you don't understand what is event driven programming and what is non blocking I/O, I will explain these with example below. Before that let's get node.js installed in your system.

To install node.js in your system,download nodejs from here https://nodejs.org/en/ and install it with install wizard.

Once nodejs is installed, go to command prompt and execute

node -v  
npm -v  

You should see the version number of nodejs.This confirms nodejs is installed in your system properly.

Let's do some cool thing with nodejs.

//import the HTTP module, this helps in creating http server
var http = require('http');

//Handler function which can be invocked on the 
function handleHTTPRequest(request, response){
    response.end('Cool! it is working, http server is up and running');

//Creating a server here
var simpleHTTPserver = http.createServer(handleHTTPRequest);

//defining the port here
const PORT=9000; 

//Lets start our server
simpleHTTPserver.listen(PORT, function(){
//this is a call back function, and gets triggered on requests
    console.log("Yay!! we are live, go ahead enter this url in browser : http://localhost:%s", PORT);

Copy above code to the file http_server.js and navigate to the folder where http_server.js file is kept and execute the command

   node http_server.js

This command executes the javascript code in http_server.js file with node runtime environment and starts the http server which is listening on port 9000.

You should see the following output in the command prompt


Head to browser and enter the url "http://localhost:9000/", You should see the response as below.

It is that easy to create a simple http server in nodejs, if you are looking for sofisticated http server with all the basic functionality you come to expect of web server (Like CRUD- Create, Read, Update and delete operations), you can look into express.js server from here, express.js is a module built on top of node.js's http module. You can get express.js using npm package manager, by executing the following command

 npm install express --save

Above code is self explainatory, initially we are requiring "http" module which helps in creating http server, in next line we are defining the function "handleHTTPRequest" to handle any incoming http requests from the browsers. In next line we are creating a server called "simpleHTTPserver" with createServer api and passing function "handleHTTPRequest" as a call back function, so that it gets invoked when ever there is a request from the browser. Finally we are asking "simpleHTTPserver" to listen on port "9000" and passing a call back function which get's invoked on successful server instatiation.

What are non-blocking I/O operations and Event driven programming?

I came across this brilliant podcast, which explains Event driven programming by drawing a parellal to waiting line at Doctor's Reception desk. Blocking I/O is like, if you are standing in the queue, receptioninst asks a guy infront of you to fill in the form and she waits till he finishes. You have to wait for your turn till the guy finishes his form, this is blocking. If single guy takes 3 mins to fill in, the 10th guy have to wait till 30 minutes. Now to reduce this 10th guys wait time, solution would be, increasing number of receptionist's, which is costly. This is what happens in traditional web servers. If you request for a user info, subsequent request by other users should wait till the current operation, fetching from Database, is completed. This increases the "time to response" of the 10th request and it increase exponentially for nth user. To avoid this traditional web servers creates thread (equivalent to increasing number of receptionists) for every single request, ie., basically it creates a copy of the server for each request which is costly interms of CPU consumption since every request will need a Operating systems thread. To scale up the app, you would have to throw lots of computation power at the app.

Event Driven: The other approach to scale up queue's "response time" is to go for event driven approach, where guy's in the queue will be handed over the form, asked to fill in and come back on completion. Hence receptionist can always take requests.
This is exactly what javascript has been doing since from it's inception. In browser, javascript would respond to user click event, scroll, swipe or database fetch and so on. This is possible in javascript, inherently, because javascript treats functions as first class objects and they can be passed as a parameters to other functions (called callbacks). This is what exactly node.js does on the server.
Node.js is single threaded and uses an event-driven model where the web server accepts the request, marks it as to be handled, and then goes on to service the next web request. When the original request is completed, it gets back in the processing queue and when it reaches the front of the queue, further processing, like executing of call back function is done. Which is more efficient than thread approach and let's you create highly-scalable webapps.

I hope this analogy helps you understand the non-blocking I/O and Event driven programming. In my next blog will dive deeper into other cool api's of node.js.