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

CH 4 Node JS

Node.js is an open source JavaScript runtime environment that allows developers to build server-side web applications. It uses asynchronous and event-driven architecture enabling highly scalable applications. The document discusses features of Node.js including its speed, scalability, modules, and use of the NPM package manager.

Uploaded by

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

CH 4 Node JS

Node.js is an open source JavaScript runtime environment that allows developers to build server-side web applications. It uses asynchronous and event-driven architecture enabling highly scalable applications. The document discusses features of Node.js including its speed, scalability, modules, and use of the NPM package manager.

Uploaded by

Ajay Kadam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CHAPTER NO 3

NODE JS
1. Introduction
 Node.js is an open source, cross-platform runtime environment and library that is
used for running web applications outside the client's browser.
 Node.js runs the V8 JavaScript engine, the core of Google Chrome, outside of the
browser.
 Ryan Dahl developed it in 2009, and its latest iteration, version 15.14, was released
in April 2021.
 Developers use Node.js to create server-side web applications, and it is perfect for
data-intensive applications since it uses an asynchronous, event-driven model.
 Node.js is used by big business and small enterprises alike. While the likes of
Amazon, Netflix, eBay, Reddit and Paypal all use it, 43% of developers using Node.JS
in 2023 do so for enterprise applications.

2. Features of Node JS
 Extremely fast: Node.js is built on Google Chrome's V8 JavaScript Engine, so
its library is very fast in code execution.
 I/O is Asynchronous and Event Driven: All APIs of Node.js library are
asynchronous i.e. non-blocking. So a Node.js based server never waits for an
API to return data. The server moves to the next API after calling it and a
notification mechanism of Events of Node.js helps the server to get a
response from the previous API call. It is also a reason that it is very fast.
 Single threaded: Node.js follows a single threaded model with event looping.
 Highly Scalable: Node.js is highly scalable because event mechanism helps
the server to respond in a non-blocking way.
 No buffering: Node.js cuts down the overall processing time while uploading
audio and video files. Node.js applications never buffer any data. These
applications simply output the data in chunks.
 Open source: Node.js has an open source community which has produced
many excellent modules to add additional capabilities to Node.js applications.
 License: Node.js is released under the MIT license.

3. Installation of node js
 You can download the latest version of Node.js installable archive file from
https://nodejs.org/en/
 check version: node –v
 To execute javascript code outside of browser use node command
 C:\AIT>node (enter)
 This command starts node environment called REPL

Vanita Patil Page 1


4. Node.js First Example
demo.js
var a=10;
console.log(a)

Open command prompt and run the following code:


d:\AIT\nodejs_ex> node demo.js

Output:
10

5. REPL
 REPL (READ, EVAL, PRINT, LOOP) is a computer environment similar to Shell
(Unix/Linux) and command prompt.
 Node comes with the REPL environment when it is installed.
 System interacts with the user through outputs of commands/expressions used.
 It is useful in writing and debugging the codes.
 The work of REPL can be understood from its full form:

 Read : It reads the inputs from users and parses it into JavaScript data structure. It is
then stored to memory.
 Eval : The parsed JavaScript data structure is evaluated for the results.
 Print : The result is printed after the evaluation.
 Loop : Loops the input command. To come out of NODE REPL, press ctrl+c twice

 Getting Started with REPL:


 To start working with REPL environment of NODE; open up the terminal (in case of
UNIX/LINUX) or the Command prompt (in case of Windows) and write node and
press ‘enter’ to start the REPL.
 The REPL has started and is demarcated by the ‘>’ symbol. Various operations can be
performed on the REPL.
 Below are some of the examples to get familiar with the REPL environment.


Vanita Patil Page 2
6. NPM (Node Package Manager)
 It is the default package manager for Node.js and is written entirely in Javascript.
 Developed by Isaac Z. Schlueter, it was initially released in January 12, 2010.
 NPM manages all the packages and modules for Node.js and consists of command
line client npm.
 It gets installed into the system with installation of Node.js.
 The required packages and modules in Node project are installed using NPM.
 A package contains all the files needed for a module and modules are the JavaScript
libraries that can be included in Node project according to the requirement of the
project.

 Installing NPM:
 To install NPM, it is required to install Node.js as NPM gets installed with Node.js
automatically.
 Checking and updating npm version:
 npm –v

 Installing Modules using npm


 npm install <Module Name>

 Global vs Local Installation


 By default, npm installs dependency in local mode. Here local mode specifies the
folder where Node application is present.
 In the global mode, NPM performs operations which affect all the Node.js
applications on the computer whereas in the local mode, NPM performs operations
for the particular local directory which affects an application in that directory only.

 1. Install Package Locally


 Use the following command to install any third party module in your local Node.js
project folder.
 C:\>npm install <package name>
 For example, the following command will install ExpressJS into MyNodeProj folder.
 C:\MyNodeProj> npm install express
 All the modules installed using NPM are installed under node_modules folder.

 2. Install Package Globally


 NPM can also install packages globally so that all the node.js application on that
computer can import and use the installed packages.
 NPM installs global packages into /<User>/local/lib/node_modules folder.

Vanita Patil Page 3


 Apply -g in the install command to install package globally. For example, the
following command will install ExpressJS globally.
 C:\MyNodeProj> npm install -g express

 3. Uninstall Packages
 Use the following command to remove a local package from your project.
 C:\>npm uninstall <package name>
 The following command will uninstall ExpressJS from the application.
 C:\MyNodeProj> npm uninstall express

7. How modules work

• Module in Node.js is a simple or complex functionality organized in single or multiple


JavaScript files which can be reused throughout the Node.js application.
• Each module in Node.js has its own context, so it cannot interfere with other
modules or pollute global scope.
• Also, each module can be placed in a separate .js file under a separate folder.
• Node.js Module Types
• Node.js includes three types of modules:
• Core Modules
• Local Modules
• Third Party Modules

• 1. 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
required 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.
• Example:
• 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);
• The following list contains some of the important core modules in Node.js:

Vanita Patil Page 4


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.

• 2. Local Modules:
• 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;
• };
• Since this file provides attributes to the outer world via exports, another file can use
its exported functionality using the require() function.

Vanita Patil Page 5


• Filename: index.js
• 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));

• Step to run this program: Run the index.js file using the following command:
• node index.js

• Output:
• Addition of 50 and 10 is 60
• Subtraction of 50 and 10 is 40
• Multiplication of 50 and 10 is 500
• Division of 50 and 10 is 5

• 3. 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

8. Webserver Creation

Vanita Patil Page 6

You might also like