What is Node.js, How it works, and how it is used in CampusNexus.

What is Node.Js, How it works

Node.js is a JavaScript framework for writing server resources dependent JavaScript applications. It is built on top of the V8 JavaScript runtime. The Main logic behind Node.js is to provide a scalable access to slow server resources, Node.js is event driven, non blocking resources model which is easy to learn. It provides a way to access resources without holding the application till its honoured.

Lets understand the Non-blocking resources with a restaurant example:

  1. In a restaurant you sit at a table and order for a coffee. The server takes the order to process it.
  2. The server goes back to the counter and places your coffee order.
  3. While the coffee is being prepared, the server will go-on to take another order.
  4. The server goes back to the counter with another order.
  5. Before the server gets your coffee you order, a sandwich.
  6. Server passes your food order to the kitchen.
  7. Your coffee is ready now, so the server picks them up and brings them back to your table.
  8. The other table’s order is ready, so the server picks them up and takes them to the other table.
  9. Finally your food is ready, so server picks it up and brings it back to your table.

 

All the requests to resources follow the same pattern. First a resources is requested, the request is placed and the server is waiting for another request without blocking the application.

Once the request is honoured the corresponding event handler is triggered and the server is ready to take another request. Notice that at no point in time is the server doing more than one thing. They can only process one request at a time. This is how non-blocking Node.js applications work.

In Node, your application code is like a server in the above application processing Coffee orders, and the coffee machine/sandwich maker is the server.

 

A single threaded JavaScript application is responsible for all the processing till it requires a Resource from the server(usually this a I/O call). At this point the call is handed off to the OS which takes care of processing the request.

Blocking resource Model

In our restaurant example, if the server would have waited for all the order (coffee and sandwich) to be completed, before taking other orders, then the customers to the restaurant would be very unhappy about the slowness in processing their orders.

Non-Blocking Resources Model

If we had to demonstrate non-blocking resource model  with our previous restaurant example, we can re-write it to precess individual Orders to without asking others to wait.

To

// requesting coffeefor table 1 and moving on…

requestCoffee([“cappuccino”], function(coffee){

return serveOrder(coffee);

});

// requesting coffee for table 2 and moving on…

requestCoffee([‘expresso’], function(coffee){

return serveOrder(coffee);

});

// requesting food for table 1 and moving on…

requestFood([‘sandwich’], function(food){

return serveOrder(food);

});

The callback functions passed as arguments are invoked asynchronously when the return value from their respective I/O calls are available. This looks like a much better restaurant to go to!

In the Browser world here is how we can relate to the above example:

  1. You use your web browser to make a request for “/about.html” on a Node.js web server. Order a Coffee
  2. The Node server accepts your request and calls a function to retrieve that file from disk. Process the Order. Place the Coffee order to the counter.
  3. While the Node server is waiting for the file to be retrieved, it services the next web request. Process Another Order
  4. When the file is retrieved, there is a callback function that is inserted in the Node servers queue. Coffee is ready at the counter.
  5. The Node server executes that function which in this case would render the “/about.html” page and send it back to your web browser. Serve the Coffee.

Here is an example how to create a server in Node.js

var server = http.createServer(); // create a server object

server.listen(8080, ‘localhost’); // begin acccepting connections

server.on(‘request’, function(req, res) {

// do something interesting with the request, write to the response

});

It is however more convenient to achieve the same result by including the optional requestListenerparameter to createServer:

var server = http.createServer(function(request, response) {

// do something interesting with the request, write to the response

});

server.listen(8080, ‘localhost’);

Event Loop Blocking Resource requests

Applications written in Node run on a single-thread. This means that they can only do one thing at a time, just like our restaurant server.

Let’s say that when your sandwich order is ready, the server needs to make sure only a certain set of vegetables are there in the sandwich:

requestFoodNonBlocking(‘vegetables in sandwich’, function(sandwich){  countNumberOfvegetables(sandwich); //blocks…  return serveOrder(sandwich);});

The server is able to pass along your food order to the kitchen in a non-blocking fashion and then proceed to do other things. However, once your order is back, they start to search and count the vegetables in the sandwich. They won’t be able to do anything else until they are done counting the beans.

If for some reason countNumberOfvegetables(vegatables) results in an infinite loop, then the server is stuck there forever and you will be forced to look for food somewhere else.

Being single-threaded means that Node.js programs can only do one thing at a time, so it is important to understand how to properly work with the event loop in order to take full advantage of the platform and avoid common pitfalls. For applications that require heavy computation and not much I/O, Node might not be your best choice.

How Node.js is used in Campus Nexus

Using NodeJs and NPM

Overview

Similar to Nuget, NPM is the package installer for NodeJs.

Tools like Bower and Gulp run on top of NodeJs and use NPM to install the tools needed.

In CampusNexus we use NPM to install Gulp.

Installing

If you have not already installed NodeJs on your computer head on over to https://nodejs.org/ and follow the simple instructions on installing the software.

NPM Install Parameters

When installing NPM pacakges, you will typically use one of three install parameters to tell NPM where and what type of package it is you are installing.

-g tells NPM to install the package globally. After you use this setting, the package that is install can be used in any project without having to install it again at the project level.

–save tells NPM that this package that can be used for both development and production. The package name and version will be added to the “dependencies” section of the package.json file.

–save-dev tells NPM that this package is only used for the development environment.  The name and version will be added to the “devDependecies” section of the package.json file.

In most cases you will want to use the –save or –save-dev parameters so the entry is added to the package.json file.  That way other developers do not have to install all the packages again separately. They just enter “npm install” from the command line, and all the packages saved in the package.json will be installed.

I will cover what is the difference between Nuget and NPM in a separate blog.

Leave a Reply

Skip to toolbar