Node.Js
On popular demand from many of my student and fellow developers, I am starting this series on Node.js . Focus will be on developing Node.js applications using Visual studio and side by side explaining things about other open source frameworks that go hand in hand with Node e.g. Bower, Grunt, Yeoman, Jasmine etc.
What is Node.js?
Node.js is a server side JavaScript framework. A server side framework means not only web framework like PHP,Asp.net but also something using which you can create your own servers like Apache or IIS. Using Node.js we can create networking applications on server as well. Unlike some other frameworks Node.js is completely Open source.
Who created Node.js?
Node.js was invented by Ryan Dahl, and other developers working at Joyent in year 2009. The first version of Node was create for Linux but later on it was adopted on window as well. In 2011, a package manager was introduced for Node.js called NPM which makes it easy to publish and share open-source Node.js libraries by the community, and simplifies installation, updating and uninstallation of libraries.
How is Node.js Server Different from IIS or Apache?
IIS and Apache are thread based i.e. every request that reaches to server is assigned a thread which takes care of executing the request. With threads comes the problem of context switching that takes lot of performance out of the system. Node.js uses event driven I/O and non-blocking I/O model that makes it lightweight and efficient.
Do we still create Threads in Node.js?
Node.js still creates thread but there is only on thread of execution which caters to all requests coming to server. Thread uses non-blocking I/O calls which are easier to write in JavaScript due to its asynchronous nature. Using a single thread means we don’t need to do context switching and moreover all the requests are executed using non-blocking I/O calls. Non-blocking I/O means any function that’s performing an I/O operation must use a callback. Having a single threaded design to cater all requests allows for creation of highly concurrent applications since there is no need to context switch.
Architecture
Node.js is built on Google V8 JavaScript execution engine. V8 was initially built for Google chrome but later open sourced by google. V8 compiles JavaScript code to machine level code. V8 is built in C++ and is dependent on native C++ libraries for asynchronous support.
How does it work?
First we create node.js server which starts to listen on a port. Whenever there is an incoming connection request to server it notifies Node.js by issuing a callback. All the connections coming to Node enters an event loop with a callback rather than assigned a new thread, and later on operating system enters the event loop at the end of callback. It’s very important to make the code as light weight as possible in Node.js server since they usually run on one server unless made a part of cluster. As a thumb rule all heavy processing code needs to be done as an asynchronous.
In next Tutorial we will discuss about installation and first program: