Edvin Cekani
Edvin Cekani
Software Developer
Edvin Cekani

Blog

Introduction to Node.JS

Introduction to Node.JS

Node.JS or Node is an open source and cross platform runtime environment for executing Javascript code outside of the browser.
Quite often we use Node to build backend services also called API (Application Programming Interface),these are services that power client applications like a web app running inside a browser or mobile app running in a mobile device.
These client apps are simply what the user sees and interacts with. They just the surface, they need to talk to some services sitting on
the server or in the cloud to store data, send emails or push notifications and so on.
Node is ideal for building highly scalable data-intense and real-time backend services that power our client applications.
Node is great for prototyping and agile development, tis also used for building super fast and highly scalable services and its used in
production by large companies such as PayPal, Uber, Netflix, Walmart ect.
In fact at PayPal they rebuild one of their Java and Spring based applications using node and it found that the node application
was build in 33% fewer lines of code and 40% fewer files and more importantly they double the number of requests served per second
while decreasing the average response time by 35%, so Node is an excellent choice for building highly scalable services.It has the
largest ecosystem of open source libraries available.So far pretty much any feature or building block you want to add to your application
there is some free open source library out there that you can use. Before node we used Javascript only to build applications that run inside of a browser. Every browser out there has what we call a javascript engine that takes our code and converts it to code that a computer can understand.
For example, Microsoft Edge uses Chakra, Firefox uses SpiderMonkey and Chrome uses V8 and its because of these varieties of engines that sometimes Javascript code can behave differently in one browser or another. A browser provides a runtime environment for javascript code. Before 2009 the only way to execute Javascript code was inside of a browser.
In 2009 Ryan Dahl the creator of node come up with a brilliant idea, he thought it would be great to execute Javascript outside of a browser, so he took Google's V8 engine which is the fastest Javascript engine out there and embedded it inside a c++ program and called that program node.
Similar to a browser node is a runtime environment for Javascript code, it contains a Javascript engine that can execute our Javascript code but it also has certain objects that provide an environment for our JavaScript code but these objects are different from the environment objects we have in the browser. In essence node is a program that includes the V8 Javascript engine plus some additional modules to give the capabilities not available inside browsers.
Both Chrome and Node share the same Javascript engine but they provide different runtime environments for Javascript.Node its not a framework, it's a runtime environment for executing Javascript code.
Node is a non-blocking or asynchronous nature, Let me give you an metaphor:
Imagine you go to a restaurant, a waiter comes to your table, takes your order and gives it to the kitchen, then they move to serve another table while the chef is preparing your meal. So the same waiter can serve many different tables, they don't have to wait for the chef to cook one meal before they serve another table, this is what we call non-blocking or asynchronous architecture and this is how node application works. The waiter is like a thread allocated to handle a request.so a single thread is used to handle multiple requests.
In contrast to non-blocking or asynchronous architecture we have blocking or synchronus architecture.
Blocking Architecture:
Imagine you go to another restaurant, and in this restaurant a waiter is allocated to you, they take your order and give it to the kitchen, now they are sitting to the kitchen waiting
for the chef to prepare your meal, at this time they are not doing anything else, they just waiting they are not going to take an order from another table until your meal is ready. This is what we call blocking or synchronous architecture and that's how applications built with frameworks like asp.net or ruby on rails works out of the box.
So when we receive a request on the server a thread is allocated to handle that request as part of handling that request it is likely that we're going to query a database and as you know sometimes it may take a little time until the result is ready, when the database is executing the query, that thread is sitting there and waiting, it can't be used to serve another client.So we need a new thread to serve another client. Imagine what will happen if we have a large number of concurrent clients at some point we are going
to run out of threads to serve these clients.So new clients have to wait until threads are available or if we don't want them to wait we need to add more hardware So with this kind of architecture we are not utilizing our resources efficiently.This is the problem with blocking or syncronous architecture and that's how applications built with asp.net work by default.
Of course in asp.net it is possible to use ansyncronous architecture but you have to do extra work for that.
In contrast node applications are ansyncronus by default.In node we have a sigle thread to handle all requests, when a request arrives that signe thread is used to handle that request, if it need to query a database,
the thread doesn't have to wait for the database to return the data, while the database is executing our query, that thread will be used to serve another client. When the database prepares the result it puts a message in what we call the event queue. Node is continuously monitoring this queue in the background. When it finds an event in this queue it will take it out and process it.
This kind of architecture makes node ideal for building applications that include a lot of disk or network access, we can serve more clients without the need to throw in more hardware and that's why node applications are highly scalable. In contrast node should not be used for CPU intensive applications like video encoding or image manipulation service, in this kind of applications we have a lot of calculations that should be done by CPU and few operations that touch the file system or the network. since node applications are single threaded when performing the calculations to serve one client other clients have to wait and thats why node should not be used for CPU intensive applications
it should only be used for building data-intensive and real time applications

Free Web Hosting