Node Js Session
Node Js Session
----------------
Node JS
----------------
What is Node.js?
Node.js is not a programming language like Python, Java or C/C++.
Need of Node.js?
Node.js can be used to full fill multiple purpose like server-side programming,
build APIs, etc.
==> Node.js is used for server-side programming with JavaScript. Hence, you can use
a single programming language (JavaScript) for both front-end and back-end
development.
==> Node.js is being used to build command line applications, web applications,
real-time chat applications, REST APIs etc.
-----------------
How to install Node JS
1. Open Nodejs.org site and download the software
2. Install it in local machine(follow instructions based on OS)
3. For the confirmation, goto command prompt, then type following
command
node -v (shows version of installed node js)
-----------------
NPM :- Node Package Manager
It is a world largest library for all script based frontend languages.
It is also Open-Source.
TypeScript
Angular
React
NodeJS
ExpressJS
VueJS
R - READ
E - EVALUATE
P - PRINT
L - LOOP
The Node.js interactive REPL terminal is like the Powershell or Command prompt
terminal, or a bash terminal in Linux. It performs the following tasks −
Read − Reads user's input, parses the input into JavaScript data-structure, and
stores in memory.
Loop − The terminal is ready to receive next input from the user.
To simplify your learning, we have set up an easy to use Node.js REPL environment
online, where you can practice Node.js syntax − To launch Node.js REPL Terminal,
visit Node.Js Terminal
To start the Node.js REPL on your computer, simply enter node in the command
terminal (without the javascript file name as done before). The Node.js prompt >
will appear.
D:\nodejs>node
Welcome to Node.js v20.9.0.
Type ".help" for more information.
>
The REPL feature of Node is very useful in experimenting with Node.js codes and to
debug JavaScript codes.
You can test any Node.js/JavaScript expression by entering in front of the >
prompt. For example −
> 10+20
30
> "Hello"+"World"
'HelloWorld'
> a=10
10
> b=20
20
> a+b
30
> Math.random()
0.5423940959293392
>
You can see that the instruction is read, evaluated, its result displayed and the
terminal is ready to receive next instruction. To start the REPL, press ctrl+c
twice, or ctrl+D, or enter .exit in front of > symbol.
Multiline Expression:-
Node REPL supports multiline expression similar to JavaScript. Let's check the
following do-while loop in action −
> x=0
0
> do {
... x++;
... console.log("x: "+x);
... }
... while (x<5);
x: 1
x: 2
x: 3
x: 4
x: 5
undefined
>
The three dots ... comes automatically when you press Enter after the opening
bracket. Node automatically checks the continuity of expressions.
-------
How to write code using VSCode:-
1. Open VSCode editor
2. Save the file with .js extension
3. Code the program as the required
4. To execute code, use below given command at terminal
node <filename.js>
-------------------------------------
Modules:-
Modules are the blocks of encapsulated code that communicate with an external
application on the basis of their related functionality.
Type of Modules
Core Modules
Local Modules
Third-party modules
Core Modules
Node.js has many built-in modules that are part of the platform and come with
Node.js installation. These modules can be loaded into the program by using the
"require()" function.
Syntax:
const module = require('module_name');
The require() function will return a JavaScript type depending on what the
particular module returns. The following example demonstrates how to use the
Node.js http module to create a web server.
----------
Local Modules
Unlike built-in and external modules, local modules are created locally in your
Node.js application. Let’s create a simple calculating module that calculates
various operations. Create a calc.js file that has the following code:
// Filename: calc.js
---
const calculator = require('./calc');
---
Third-party modules
Third-party modules are modules that are available online using the Node Package
Manager(NPM). These modules can be installed in the project folder or globally.
Some of the popular third-party modules are Mongoose, express, angular, and React.
Example: