Introduction to NODE.JS

What is NODE.JS?


  • Node.js is a JavaScript run-time.The language used is JavaScript.So Node.js is not a language or a framework or some library.So basically it's JavaScript running in your machine instead of browser which what was used to be done traditionally.
  • It's been built on the V8 JavaScript engine(same engine Google Chrome is built on) .Node and V8 is mostly written in C++ language which is also very efficient and fast.
  • So all this essentially means that we can use JavaScript as a server side language.

Why  use NODE?


  • Fast, efficient and highly scalable.
  • Event driven,non-blocking I/O model.(will be explained down below)
  • Popular in the industry.
  • Same language on the front-end and back-end.


NON-BLOCKING I/O used by NODE

Node.js is non-blocking and asynchronous, It runs on a single thread using non-blocking I/O(Input/Output) calls.This single thread can support thousands of connections which are held in what called an event-loop.This method optimizes throughput & scalability in apps with many I/O operations.It is also important to note that Node.js is advised not be used with CPU intensive apps.


NODE's EVENT LOOP


This is a diagram of the event loop and how it works.Here events are run asynchronously.Basically when an event is triggered a call back fires so the system doesn't have to wait until that event is over so it simply moves on after firing an event and once than event is fulfilled it runs in the loop.


Best Types Of Projects for NODE


  • REST API & Micro-services.
  • Real Time Services (Chat, Live updates).
  • CRUD Apps - Blogs, Shopping Carts,Social Networks.
  • Tools & Utilities. 


Basically any application that is not CPU intensive and has a lot of I/O operations is fine.A CPU intensive application can block the whole server and make the operations very slow.



NPM - NODE PACKAGE MANAGER 

When you download and install Node.js you also get something called Node package manager.This is used for,

Install 3rd part packages (frameworks,libraries, tools, etc).

When you install these packages they get stored in a folder called "node_modules" in your project folder.Also any dependency that you install will be listed in a file named "package.json".You can also create NPM scripts to run certain tasks such as  run a server.

Some basic Node commands,

npm init                                           - Generates a package.json file

npm install express                          - Install a package locally

npm install -g nodemon                   - Install a package globally  


NODE Modules


  • Node core modules (path, fs, http, etc)
  • 3rd party modules/packages install via NPM
  • Custom modules (create your own modules and use export to use it.This way you can use variable,functions,classes etc) .









Comments

Popular posts from this blog

What is a Callback? - JavaScript

Difference between var,const and let in JavaScript