0% found this document useful (0 votes)
10 views

Node Js Session

Uploaded by

Rushikesh goud
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Node Js Session

Uploaded by

Rushikesh goud
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Today's Agenda

----------------
Node JS
----------------
What is Node.js?
Node.js is not a programming language like Python, Java or C/C++.

Node.js is a runtime enviornment, similar to Java virtual Machine, that converts


JavaScript code into machine code.

It is , widely used by thousands of developers around the world to develop I/O


intensive web applications like video streaming sites, single-page applications,
and other web applications.

Node js => Runtime Env + Javascript

It is possible to use JavaScript as a backend.

With JavaScript already being a popular choice for frontend development,


application development around MERN (MongoDB, Express, React and Node.js) and MEAN
(MongoDB, Express, Angular and Node.js) stacks is being increasingly employed by
developers.

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 implements asynchronous execution of tasks in a single thread with


async and await technique. This makes Node.js application significantly faster
than multi-threaded applications.

==> 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

for these all the languages, it will provide library.


to work with npm, it will provide a command prompt called as CLI-Command Line
Client
here there is a command called as "npm".

with npm command, the required packages can installed.


npm install <package name>
-----------------
REPL:-
The Node.js runtime has a built-in interactive shell, in which you can execute
instructions one at a time.
The Node.js interactive shell works on the principle of REPL, which is an acronym
for

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.

Eval − Takes and evaluates the data structure.

Print − Prints the result.

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.

Modules can be a single file or a collection of multiple files/folders.

The reason programmers are heavily reliant on modules is because of their


reusability as well as the ability to break down a complex piece of code into
manageable chunks.

We will discuss the different types of Node.js Modules:

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.

const http = require('http');

http.createServer(function (req, res) {


res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('Welcome to this page!');
res.end();
}).listen(3000);

Core Modules Description


------------------------------------
http creates an HTTP server in Node.js.
assert set of assertion functions useful for testing.
fs used to handle file system.
path includes methods to deal with file paths.
process provides information and control about the current Node.js
process.
os provides information about the operating system.
querystring utility used for parsing and formatting URL query strings.
url module provides utilities for URL resolution and parsing.

----------
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

exports.add = function (x, y) {


return x + y;
};

exports.sub = function (x, y) {


return x - y;
};

exports.mult = function (x, y) {


return x * y;
};

exports.div = function (x, y) {


return x / y;
};

---
const calculator = require('./calc');

let x = 50, y = 10;

console.log("Addition of 50 and 10 is "


+ calculator.add(x, y));
console.log("Subtraction of 50 and 10 is "
+ calculator.sub(x, y));

console.log("Multiplication of 50 and 10 is "


+ calculator.mult(x, y));

console.log("Division of 50 and 10 is "


+ calculator.div(x, y));

---
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:

npm install express


npm install mongoose
npm install -g @angular/cli
Example: Installing and Using Express

npm install express


---

You might also like