So lets start with the basic and boring defination of Node.js
Node.js
Node.js is a javascript runtime built on google's open source v8 javascript engine
wait I just forgot to tell you the pre-requisite to learn Node .js
You must be familiar with the javascript ES6 classes
You should know about javascript Promises and callbacks , Async and Await
** Don't worry If you don't know about these stuff , **
**they will hardly take your one hour to learn **
What is Node.js?
Node.js is a javascript framework used to make some fast and scalable websites , you can create some amazing stuffs like video streaming webapp , real-time chat application and many more now a days node.js is very popular among the developers and why not it is fast , it is scalable , it is everything you need to build the backend of a website . It is perfect for building super-fast data-intensive web applications
When to use Node.js ?
API with database behind it
Data Streaming (You tube )
Real-time chat app
Server-side web applications
Where we should avoid using node.js?
-Application with heavy server-side processing (CPU-Intensive)
- we should use Ruby rails, php In these kind of situations
** Most important feature of node.js is we can execute javascript outside the browser . **
Let's learn about some modules in node
File system :
this module we use to read or write files.
How to import this module?
we can import this or any module in node by using require method syntax : const variable_name = require('fs') fs is the keyword to import this module . Learn more about fs module on : nodejs.org/api/fs.html
Methods to read and write file synchornously
readFileSync() : this method used to read any file whether it is a txt file or html file
writeFileSync() : this method used to write a existing file or if a file is not present then this method will create a new file .
Lets move to our next module HTTP
http module is used to take request from the user and send the response . We can also create our own server with the help of http by using http.createServer() method .
Blocking and Non-Blocking Asynchronous nature of node.js
** Synchronous : **
each statement is processed by one after another , In this nature your program gets executed line by line . There are lots of flaws in this approach because when we write a code or suppose we have to read a big txt file and then we have to execute some other functions . but It is taking a lot of time to read the file thats why my next code is waiting to get executed and I am not able to do any work till the file is finished executing . It will create a load on server .
But how to solve this problem ?
answer is asynchronous nature :
So in asynchronous approach we upload the heavy work to be worked in the background . What happens in Asynchronous approach is basically if we have to read a big file then it will wrap it in the call back function so until we finish executing the first task asynchronous will move to the next task in the code . The heavy task will be offloaded to the background , so we are not blocking any code in Asynchronous method .
what is the benifit of using Asynchronous ?
because Node.js is single threaded so each app will have only one single thread. So it might work in with 5 users but it will be very difficult if you are dealing with 5 million users. each user will have to wait till the first user finish it .
Asynchronous method
readFile()
writeFile()
Routing , exporting modules , npm introduction , package versioning and updating , Third party modules , HTML templating
I will add these things in next blog/article