Node Js or Node is an open source and cross-platform runtime envirnonment for executing JavaScript code outside of a browser. Quite often we use node to build back-end services also called apis or application programming interfaces, these are the services that power our client apllications like a web app running inside of a web browser or a mobile app on a mobile device. These client apps are simply what the user sees and interact with, They're 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, kick off workflows,...
Node is ideal for building Highly-scalable, data-intensive and real-time apps/back-end sevices that power our client applications. Now you might ask but there are other tools and frameworks out there for building back-end services such as asp.net, rails & Django,... So what's special about Node?
- Well
Nodeis easy to get started and can be used forprototypingandagile development, but it can also be used for buildingsuper fastandhighly scalableservices. It is used in production by large companies such asPayPal,Uber,Netflix,Walmart,... In fact atPayPalthey rebuilt one of theirJavaandspringbased applications usingNode Jsand they found that theNodeapplication was built twice as fast with fewer people in 33% fewer lines of code and 40% fewer files and more importantly they doubled the number requests served per second while decreasing the average response time by 35%. SoNodeis an excellent choice for buildinghighly scalableservices
- Another reason for using
Nodeis that in old applications we useJavaScript, so if you're a Front-end Developer and knowJavaScriptyou can reuse yourJavaScript skillsand a Transtion to aFull-stackdeveloper and get a better job with a better pay! You don't have to learn a newprogramming languagealso because you can useJavaScriptboth on the Front-end and on the Back-end yoursource codewill be cleaner and more consistent, so you will use the samenaming conventions, the sametoolsand the samebest practices - And finally another reason for using
Nodeis that it has the largestecosystemofopen source librariesavailable to you! So for pretty much any features or building blocks you want to add to yourapplication, there is some freeopen source libraryout there that you can use. So you don't have to build thisbuilding blocksfrom scratch and instead you can focus on the core of yourapplication.
So above we learnt that Node is a runtime environment for executing JavaScript code, but what is a runtime environment really? Well before Node we used JavaScript only to build applications that run inside of a browser, so every browser out there has what we call a JavaScript Engine That takes our JavaScript code and converts it to code that a computer can understand.
- For example
Micosoft Edgeuseschakra,FirefoxusesSpiderMonkeyandChromeusesV8.
- And it's beacause of these varieties of
enginesthat sometimesJavaScript codecan behave differently in onebrowseroranother. Now abrowserprovides a runtime environment forJavaScript code
- For instance you probably know that in
browserswe have thewindowor thedocumentobject, theseobjectsallow us to work with theenvironmentin which ourcodeis running.
document.getElementById('');- Now up to
2009the only way to executeJavaScript codewas inside of abrowser.- In
2009Ryan Dahlthe creator ofNodecame up with abrilliant idea, he thought it would be great to executeJavaScriptoutside of abrowser, so he tookGoogle's V8 Enginewhich is the fastestJavaScript Engineout there and embedded it inside aC++ programand called that programNode. So similar to abrowserNodeis a runtime environment forJavaScript code, it contains aJavaScript Enginethat can execute ourJavaScript code, but it also has certainobjectsthat provide an environment for ourJavaScript code. But theseobjectsare different from theenvironment objectswe have inbrowsers, for example:- We don't have the
document object, instead we have otherobjectsthat give us more interesting capabilities, for example; We can work withfile system, listen forrequestson a givenport,... We can't do stuff like that inside of abrowser, right?
- We don't have the
- In
browser's object:
document.getElementById('');Node's objects:
fs.readFile()http.createServer()Ryan Dahl'sbrilliant idea:
- In
essenceNodeis aprogramthat includes theV8 JavaScript Engineplus some additionalmodulesthat give us capabilities not available insidebrowsers, we can work with thefile systemor thenetwork,... BothChromeandNodeshare the sameJavaScript Engine, but they provide different runtime environments forJavaScript.- Some people compare
NodetoC#orRubyor some otherProgramming Languages, but these comparisons are fundamentally wrong! BecauseNodeis not aprogramming language, it's like comparing acarwith anapple. By the sametokenNodeshould not be compared withframeworkssuch asASP.NETorRailsorDjango,... These areframeworksfor buildingweb applications.Nodeis not aframework, it's a runtime environment for executingJavaScript code
- Some people compare
Above it's mentioned that Node applications are Highly-scalable and this is because of the Non-blocking or ASYNCHRONOUS nature of Node, what does it mean by ASYNCHRONOUS here is a 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 on to serve another table while the chef is preparing your meal, so the same person 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 applications work. The waiter is like a thread allocated ho 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 SYNCHRONOUS architecture, let's see how this one works; So back to our restaurant example imagine you go to another restaurant and in that restaurant a waiter is allocated to you, they take your order and give it to the kitchen now they are sitting in the kitchen waiting for the chef to prepare your meal at this time they're not doing anything else, they're just waiting, they're 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 Rails work 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 requests it is likely that we're going to query a database and as you know sometimes it may take a little while until the result is ready, when the database is executing the query that thread is sitting there waiting. It can't be used to serve another client, so we need a new thread to serve another client.
Non-blockingORASYNCHRONOUSArchitecture
Node.mp4
BlockingORSYNCHRONOUSArchitecture
Node1.mp4
Now imagine what would happen if we have a large number of concurrent clients, at some point we're going to run out of threads to serve these clients, so new clients have to wait until three 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 SYNCHRONOUS architecture and as expalined that's how applications built with frameworks like ASP.NET work by default of course in ASP.NET, it is possible to use ASYNCHRONOUS architecture but you will have to do extra work for now, in contrast Node applications are asynchronous by default, so you don't have to do anything extra.
In Node we have a single thread to handle all requests, when a request arrives that single thread is used to handle that request, if you need to query a database or 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 an Event queue.
Node Js 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
clientswithout the need to throw in morehardwareand that's whyNode applicationsare Highly-scalable.
- In contrast
Nodeshould not be used forCPU-intensiveapplications like avideo encodingor animage manipulation service, in this kind of applications we have a lot of calcuations that should be done byCPUand fewoperationsthat touch thefile systemor thenetwork
- Since
Node applicationsare singlethreadedwhen performing the calculation to serve oneclientotherclientshave to wait, and that's whyNodeshould not be used forCPU-intensiveapplications. It should only be used for buildingdata-intensiveandreal-timeapplications.

























