Get started with Node JS

Get started with Node JS

Node JS, as per the standard definition is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. The term runtime is used to indicate various custom functionalities like tools and libraries that is specific to the environment. It's built on chrome's V8 engine. Every browser has it's own engine that runs javascript, and in chrome V8 is the one.

Untitled.png source: udemy

Chrome is extensively built on c++ and the same applies to Node JS. Before Node JS came into existence, javascript ran only in browser and there was no other way. With the help of browser, we could only perform DOM manipulation. JavaScript can’t be used to interact with file system. If there is a query written in JS that is meant to fetch some records from a file, it’s actually not JS that is doing the favor but c++ that is helping us out. But the same was not the case with c++ and other programming languages, wherein interaction with file system could easily be performed. This was a drawback of javascript. Engineers then analyzed the problem and came up with something call Node JS, where javascript could not only be run on browser but also on an external command line.

Development of node js was indeed a revolution. Since node js is built using javascript, it's highly efficient and has become darling of web developers. Since web developers learn Javascript/ typescript in their early days as a part of web development basics, it becomes easier for them to learn node js and become efficient in backend as well. It's often misunderstood that node js is a backend framework that is used in building servers but that's not the case. It can also be used on frontend.

Node js is primarily used for non-blocking, event driven servers due to it's single threaded nature. Node js uses asynchronous (sequence of events doesn't matter) mode of operation and avoids threads for each process. Non blocking essentially means that the server will not block itself for one request and runs continuously while handling the other client requests. Single threaded on the other hand means that it has only one call stack and whichever is on top of the call stack is run first and so on. Node js is mostly used in developing applications that require persistent connection. This is one of the reasons why node js is extensively preferred in building real time chat applications.

Advantages of Node js

One of the primary advantages of node js lies in using javascript for writing both frontend and backend. However, there are some other benefits too,

  1. Easy to learn
  2. Scalability
  3. Robustness and,
  4. Great ecosystem

A note on node package manager :

Node package manager, in other words NPM, is one of the world's largest software registry that houses around 8-10 lakh code packages.

npm.png

NPM includes a command line interface that can be used to download and install various packages. These packages so installed are then defined in a file named package.json where they are stored as key value pairs. While developing an application, we come across various issues for which we might have to write another set of code just to get rid of the problems and that code is in no way making the application effective. To avoid writing extra lines of code for every problem we encounter, we have packages that help us out.

Eg: we might have to parse an external data from one datatype to another & in that case, we have Yargs package which can come handy to us. Using Validator package, we can validate the input data and then we have Chalk package which could be used to colorize the code lines.

Similarly, we have n number of packages which could help us write better code.

To understand more about Node and it's related modules, go though the official Node documentation https://nodejs.org/en/docs/.