Node.js : The Complete Guide to Build RESTFUL APIs

Monday, December 23, 2019

Node.js : The Complete Guide to Build RESTFUL APIs



Node JS is a platform, built on Crome's JavaScript runtime for easily building fast and scalable network applications. Node JS uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. 

Node JS

1. Overview of Node JS


Developed By -  Node JS developed by Ryan Dahl, 27 may 2009.

Features - 
  1.      Node JS is server side javascript programming language.
  2.      Node JS is an open source server environment. (That mean's it completely free)
  3.      Node JS can runs on various platform like windows, linux, mac OS, Ubuntu e.t.c
  4.      Node JS supports the non blocking IO.
  5.      Node JS supports promises e.t.c

What is Node JS ?


The modern web application has really come a long way over the years with the introduction of many popular frameworks such as bootstrap, Angular JS, Angular 2+,4+ e.t.c Vue JS, React JS etc. All of these frameworks are based on the popular JavaScript framework which we are using from client side.


But when it came to developing server-based applications, there was a kind of void, and this is where Node JS came into the picture.


So what is Node JS ?



- Node JS is an Asynchronous event-driven I/O operations which helps to handle concurrent request – This is probably the most significant selling point of Node.js. This feature basically means that if a request is received by Node for some Input/Output operation, it will execute the operation in the background and continue with processing other requests.

- Node JS is basically server side programming language, which runs on top google v8 JIT compiled javascript engine.


More Details please watch the below video link -


2. Client side vs Server Side


The client-side environment used to run scripts is usually a browser. The processing takes place on the end users computer. The source code is transferred from the web server to the users computer over the internet and run directly in the browser.

Client side vs server side
Client side vs Server side


The server-side environment that runs a scripting language is a web server.In very simple way, Client not able to interact with the other client for that reason they need to have a dedicated server, where server side programming language came into the picture.

When we say server side, we are talking about java, node js, PHP, .net e.t.c

Client side -- Browser side (Crome, Firefox, safari, opera, internet explorer e.t.c)
Example Programming language- AngularJS, HTML, Bootstrap, Angular4+, React, Vue Js e.t.c 

Server side - .net, java, php, node JS e.t.c


3. Why Node JS ?


As we have discussed earlier, Node js provides a non blocking IO, where request can be handled in asynchronous manner.

So why node js, I bring some basic difference.

why node js
Why Node js ?




More Details please watch the below video link -




       

4. Why Node JS is Non Blocking IO ?



As we have discuss earlier, Node JS provides a non blocking IO, where request can be handled in asynchronous manner. 

Synchronous I/O vs Asynchronous I/O
Synchronous I/O vs Asynchronous I/O



So we have discuss about synchronous and Asynchronous I/O.



5. Installation of Node JS ?



Node JS
NVM
Visual Studio


Please download this project link - 

git clone https://atique1224@bitbucket.org/atique1224/youtube_nodejs.git

More Details & hands on please watch the below video link -



 6. Why Node JS is Single thread ?



why node js is single thread ?
Why node JS is single thread ?





So, In real time example, in restaurants waiter comes to your table - 1 takes the order, push the order to kitchen, So until unless chef process your order and serve to table 1, waiter is not going to take the order from table 2,  --- This kind of blocking IO as we discussed earlier.

In non blocking part - what happens waiter takes the order from table 1, push to the kitchen and waiter again comes to the table 2 to take the order, while chef preparing food for table-1, So this way they can serve many request at a time. 

7. How Node JS worked as a Single thread - Example ?



node js single thread
Single thread




So let’s discuss how Node JS work as a single thread. So here Once the request came from the client side, It assign to a thread. If that request needs database/File operation then it push to eventQueue, and process the next request. Once it got the result from the event queue, immediately it provide result to the request. So Node JS is continuously monitoring this Event queue background.



8. What is Event Driven Programming - Node JS ?




Event Driven Programming in Node JS
Event Driven Programming in Node JS


In computer programming, event-driven programming is determined by events such as user actions (mouse clicks, key presses), sensor outputs, or messages from other programs or threads. For the practical example please check my below video.

Please download this project link - 

git clone https://atique1224@bitbucket.org/atique1224/youtube_nodejs.git

More Details & hands on please watch the below video link -




   

9. SetTimeout vs SetInterval



setTimeout - setTimeout is a native JavaScript function. Which calls a function and pieces of code after getting some delay.



setIntervalsetInterval basically calls the method for a at specific intervals.



10. SetImmediate vs ProcessNextTick

setImmediate - Any function passed as the setImmediate() argument is a callback that’s executed in the next iteration of the event loop.

process NextTick - A function passed to process.nextTick() is going to be executed on the current iteration of the event loop, after the current operation ends. This means it will always execute before setTimeout and setImmediate. For the practical example please check my below video.

Please download this project link - 

git clone https://atique1224@bitbucket.org/atique1224/youtube_nodejs.git

More Details & hands on please watch the below video link -



11. What is Callback's in Node JS ?



A callback is a function that is to be executed after another function has finished executing — hence the name 'call back'. More complexly put: In JavaScript

Any function that is passed as an argument is called a callback function. For the practical example please check my below video.



Please download this project link - 


git clone https://atique1224@bitbucket.org/atique1224/youtube_nodejs.git

More Details & hands on please watch the below video link -





12. What is Promises in Node JS ?

In short I can say, During the development lifecycle, there may be an instance where you would need to nest multiple callback functions together. This can get kind of messy and difficult to maintain at a certain point in time. Where promises came to the picture. Let’s do some hands on coding to see how the promises work. More details please watch below video.

Please download this project link - 

git clone https://atique1224@bitbucket.org/atique1224/youtube_nodejs.git

More Details & hands on Please watch the below video link -



13. What is Async, Async Await, Async waterfall, Async Parallel in Node JS ?


Async - Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript. Although originally designed for use with Node JS.

Async await - With Node v8, the async/await feature was officially rolled out by the Node to deal with Promises and function chaining. The functions need not to be chained one after another, simply await the function that returns the Promise. But the function async needs to be declared before awaiting a function returning a Promise.

Async waterfall - It Runs an array of functions in series, each passing their results to the next in the array. However, if any of the functions pass an error to the callback, the next function is not executed and the main callback is immediately called with the error.

Async parallel - Async parallel() is a collection of the asynchronous functions to run (an array, object ). Each function is passed a callback(err, result) which it must call on completion with an error err (which can be null ) and an optional results value. The optional second argument to async.


Please download this project link - 

git clone https://atique1224@bitbucket.org/atique1224/youtube_nodejs.git


More Details & hands on Please watch the below video link -




14. What is Async Priority queue & Async Race in Node JS ?



Async priority - Based on the priority function will run.

Async Race - The result of the first complete execution is passed. It may be the result or error.


Please download this project link - 



git clone https://atique1224@bitbucket.org/atique1224/youtube_nodejs.git



More Details & hands on Please watch the below video link -







15. Express in Node JS ?


Express - Express is a Node JS application framework, which is free and open source, It is design for building web application and API.


Similar framework like express - hapi.js, socket.io, Koa, derby.





16. How Express Implement in Node JS ?


Express in Node JS
Node JS with Express


More Details - Please download this project link - 

git clone https://atique1224@bitbucket.org/atique1224/youtube_nodejs.git


More Details & Practical Explanation Please watch the below video link -