What is Node ?

What is node?

Breaking the borders

No doubt, there has been a huge hype around Javascript for years now. It seems like Javascript is everywhere. You can create awesome mobile apps with it. Some crazy guys even created a project where C code will be translated into Javascript( emscripten )

If you can create singlepage-apps that works like a native application… Why shouldn’t you use Javascript on the server as well? That’s where node.js comes into play. I won’t talk about whether it’s a good idea to use an untyped language on the server or not. This decision belongs to you.. But… choose wisely;))

Node is written in C++ (as well as Google’s V8 Engine), runs on Googles V8 and is part of the Server Side JavaScript environment. It extends the JavaScript API to offer usual server side functionalities. Node has been created by Ryan Dahl back in 2009. The project is run by Joyent (the company employing Ryan Dahl). It’s an opensource project.

Node’s internal architecture :

node_standard1 (1)

 

The goal :

“Node.Js is a platform built on Chrome’s JavaScript run time for easily building fast, scalable network applications.” That’s what the website says. It should be easy to use, and also very flexible.

There are 3 major feature in node’s architecture, to archive this goal.

1 ) Single threaded : 

Unlike the most (web)server which spawns a thread for every incoming request, node only uses one thread. This avoids context switching. You may think, 1 thread sounds like a bottle neck? Well consider reading about the C10k problem and go on reading ;)..

2) Non blocking I/O :

Javascript is by design a non blocking language, so node uses it as one of it’s main features to avoid waiting for a response of long running processes. (e.g. database calls). This allows node to handle a huge amount of traffic because the process will be handed over and the main thread is free to do new stuff..

But wait.. didn’t I just say it’s single threaded… Well in the one hand it is.. In the other it’s not.. Let’s have a closer look.

3) Event Loop ( Node_threading_model) :

The (single threaded) event loop uses Marc Lehmann’s libev. Actually, the event loop is indeed single threaded. To be able to answer to new incoming requests as soon as possible, long running jobs will be handed over to internal worker threads.When doing slow I/O operations such as reading a database,the program does not wait.Instead,it immediately continues to the next line of code.When the I/O operation returns,it triggers a callback function and the result can be processed.If the

Summary:

I’ve explained how node works internally and what kind of technique is used to achieve the goals mentioned above. Node actually is a Javascript runtime without a DOM, but with a lot of API’s, you would wish to have in a browser. One of the main features in my opinion is that you can do nearly everything, but you don’t have to. E.g you can create a little webserver that’s really just printing out “Hello World”. This webserver plus the actual “logic” would contain no more that 8 lines of code.

More information on Node.Js

About

My Name is Dileep Singh, Noder, Javascript Lover & NoSql Developer, Fitness Freak, Love to travel new places, learning photography & Music Manic ♯ ♩ ♬

Posted in Node.Js Tagged with: ,