Node JS - Basics

Node JS - Basics

Key concepts about NodeJs

·

5 min read

NodeJS:

When and Why nodeJS was introduced ? NodeJs was introduced by Rayn Dahl in 2009 using Chrome V8 Engine and C/C++ language. Before nodeJS was introduced, javascript can be only used for front end frameworks and can be run only inside the web browsers.But after the introduction of nodeJS,JavaScript can be used for both front and back-end developing and also can be run outside and inside the web browsers.

Why nodeJS is preferred?

                Easy to Use 
                Highly scalable
                Faster 
                Less Hiring

Blocking and Non-Blocking in nodeJS : Blocking : It is a process which block other operations until that particular operation executes and finishes the task.From the below image, we can observe that data1 is stored and then printed and then data2 stored and printed and these two data's is added up and their sum is printed.

IMG_20210301_235906.jpg

IMG_20210302_000415.jpg

Non blocking : It is a process which doesn't block anything.

IMG_20210302_000607.jpg

IMG_20210302_000937.jpg

Node REPL : It is an interactive shell that processes the node.js expressions.REPL stands for :: R- Read (Reads the javascript code that is entered in the node shell/ command prompt) E-Evaluate(Evaluates the result of the code entered before.) P-Print(Prints the result to the user) L-Loop(Loops until the user signals to quit)

IMG_20210302_001800.jpg

Some basic commands used in nodeJS: .help , .break , .clear , .editor , .exit , .load , .save

Built-in modules in nodeJS: You can find some of the built-in nodeJS modules here:

https://nodejs.org/dist/latest-v14.x/docs/api/fs.html

What is npm? npm stands for Node Package Manager. npm is package from which we can import a module which is created by other developers. npm is installed by default in nodeJS.With help of npm we can, Share code with others Reuse the code in other projects. Use the code written by others.

For example : chalk module in npm changes the color of the given text into any other colours which we want ! .

Node OS module : Node OS module is the one which tells us more about our operating system. For example : In the image given below os.type() returns the type

IMG_20210302_174050.jpg

Process Module:
"Process" object is the global object in node js.If you run through the following commands in node shell , we wil obtain as follows : process.env - returns all the variables of the session including the user. process.env.USERNAME - returns the user. process.env.var= 13 - returns 13 as "var" is variable name.If you want to delete the variable just use delete process.env.var which will delete the variable.

Hello server : We can create servers as like follows :

IMG_20210302_173851.jpg

Hello Express :
Express is a framework in npm.We can build web apps using express,To use express in our projects we have to install it and then use it.

Installation command:

            >  $ npm install express --save

To know further about Express Js you can refer the below link:

              [ExpressJs](https://expressjs.com/) 

Nodemon : It is a tool that helps developers of nodeJS applications by automatically restarting the node.

      **To install nodemon :**

Either through cloning with git or by using npm (the recommended way)

npm install -g nodemon

And nodemon will be installed globally to your system path. You can also install nodemon as a development dependency:

npm install --save-dev nodemon

And to proceed further you can refer this resource: Nodemon