0% found this document useful (0 votes)
11 views87 pages

Lecture Notes

The document provides lecture notes for the IT3501 Full Stack Web Development course at St. Peter's College of Engineering & Technology, affiliated with Anna University. It covers the basics of full-stack development, including front-end and back-end development, the MVC architecture, and the roles of various components in web application frameworks. Additionally, it discusses the advantages and disadvantages of the MVC model and the functions of web browsers and servers in the web development process.

Uploaded by

jemimaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views87 pages

Lecture Notes

The document provides lecture notes for the IT3501 Full Stack Web Development course at St. Peter's College of Engineering & Technology, affiliated with Anna University. It covers the basics of full-stack development, including front-end and back-end development, the MVC architecture, and the roles of various components in web application frameworks. Additionally, it discusses the advantages and disadvantages of the MVC model and the functions of web browsers and servers in the web development process.

Uploaded by

jemimaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 87

lOMoARcPSD|34806321

IT3501 FSWD Notes

Full Stack Web Development (Anna University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by gowsalyaa m ([email protected])
lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

DEPARTMENT OF INFORMATION TECHNOLOGY

Subject Code//Name:IT3501/FULL STACK WEB DEVELOPMENT

Year/Sem: III/V

LECTURE NOTES

(Regulation 2021)

Academic year :2024-2025

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

IT3501 Full Stack Web Development


Introduction
Full-stack A stack refers to all of the components needed to run an application. This includes
frameworks and components internal to an application, such as UI frameworks, MVC frameworks, data
access libraries, etc. It can also include things external to an application, such as the operating system,
application server, and database. A full-stack web developer is a developer that has general knowledge
across a wide breadth of technologies and platforms as well as in-depth experience and specialization in a
couple of those concepts. For the most part, there are two general fields that make up a full-stack developer’s
skillset: front-end development and back-end development.

Front-End Development This skillset involves the actual presentation of your website—how the
information in your website is laid out in browsers and on mobile devices as well. A dedicated front-end
developer will be very experienced working with HTML and CSS as well as the scripting language, JavaScript.
With these languages, the developer can very efficiently manipulate the information on a website to make
it appealing and effective. Everything that you actually see on a website—the layout, the positioning of text
and images, colors, fonts, buttons, and so on—are all factors that the front-end developer must consider.

The main goal of a front-end developer is to provide the platform for visitors to interact with, a
platform which provides and receives information. This means some developers will be wellversed in web
design and using software such as Photoshop and Illustrator to create graphics and themed layouts.
Additional skillsets of a front-end developer could include user experience design and user interface design,
skills which help a team evaluate the best methods of displaying and collecting information. A front-end
developer who possesses these design skills is potentially more valuable as they can identify the look and
feel of a site while assessing the technical capabilities of such a design at the same time.

Back-End Development Create, edit/update and recollection of data are some of the processes that
are most often associated with back-end development. Some examples of common scripting languages used
are JavaScript, PHP, Ruby, and Python. With these languages, a back-end developer can create algorithms
and business logic to manipulate the data that was received in front-end development. This means that a
back-end developer must be able to write code to receive the information input from the user and also save
it somewhere – like in a database. There are two main types of databases: ➢ Relational (Oracle, SQL Server,
PostgreSQL, MySQL) ➢ Non-relational (Mongo, Oracle, Couchbase) The language used for database
management is SQL, which helps the developer interact with the database. The concepts might sound
foreign, but just understand that there are different database management systems based on convenience
and use. Another component of back-end development is server management, which are applications that
host the database and serve up the website. An alternative to knowing how to manage servers is to use
cloud-based platforms that provide the infrastructure, like Heroku or Amazon Web Services. Understanding
server management allows a developer to troubleshoot slow applications and even determine how scalable
their websites are to include more users.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

UNIT I

BASICS OF FULL STACK


Understanding the Basic Web Development Framework – User – Browser – Webserver – Backend Services – MVC Architecture – Understanding
the different stacks –The role of Express – Angular – Node – Mongo DB – React

Understanding the Basic Web Development Framework

A development framework is a set of tools, libraries and conventions that enable developers to create
applications more quickly and efficiently. Development frameworks provide structures and templates for
organizing application code and files, as well as common features such as database management, user input
validation, user session management and security.

Development frameworks are usually designed for a specific programming language, such as Ruby on Rails
for Ruby, Django for Python, or Laravel for PHP. They allow developers to focus on creating the business
logic of the application rather than the technical details of the underlying infrastructure, which can
significantly speed up application development and maintenance.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Web development framework working


A web development framework usually follows an MVC (Model-View-Controller) architecture model to
organize the code and files of the application. The MVC model divides the application into three main parts:

The Model-View-Controller (MVC)

• The model is responsible for the business logic and data of the application.

• The view manages the application’s user interface.

• The controller establishes and maintains communication between the model and the view and manages the
application logic.

Components of MVC

The MVC framework includes the following 3 components:

• Controller

• Model

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

• View

MVC Architecture Design

Controller:

The controller is the component that enables the interconnection between the views and the model so it acts
as an intermediary. The controller doesn’t have to worry about handling data logic, it just tells the model
what to do. It processes all the business logic and incoming requests, manipulates data using
the Model component, and interact with the View to render the final output.

Responsibilities:

• Receiving user input and interpreting it.

• Updating the Model based on user actions.

• Selecting and displaying the appropriate View.

Example: In a bookstore application, the Controller would handle actions such as searching for a book,
adding a book to the cart, or checking out.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

View:

The View component is used for all the UI logic of the application. It generates a user interface for the user.
Views are created by the data which is collected by the model component but these data aren’t taken directly
but through the controller. It only interacts with the controller.

Responsibilities:

• Rendering data to the user in a specific format.

• Displaying the user interface elements.

• Updating the display when the Model changes.

Example: In a bookstore application, the View would display the list of books, book details, and provide input
fields for searching or filtering books.

Model:

The Model component corresponds to all the data-related logic that the user works with. This can represent
either the data that is being transferred between the View and Controller components or any other business
logic-related data. It can add or retrieve data from the database. It responds to the controller’s request
because the controller can’t interact with the database by itself. The model interacts with the database and
gives the required data back to the controller.

Responsibilities:

• Managing data: CRUD (Create, Read, Update, Delete) operations.

• Enforcing business rules.

• Notifying the View and Controller of state changes.

Example: In a bookstore application, the Model would handle data related to books, such as the book title,
author, price, and stock level.

Working of the MVC framework with Example

Let’s imagine an end-user sends a request to a server to get a list of students studying in a class. The server
would then send that request to that particular controller that handles students. That controller would then
request the model that handles students to return a list of all students studying in a class.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

The flow of Data in MVC Components

The model would query the database for the list of all students and then return that list back to the controller.
If the response back from the model was successful, then the controller would ask the view associated with
students to return a presentation of the list of students. This view would take the list of students from the
controller and render the list into HTML that can be used by the browser.

The controller would then take that presentation and returns it back to the user. Thus ending the request. If
earlier the model returned an error, the controller would handle that error by asking the view that handles
errors to render a presentation for that particular error. That error presentation would then be returned to
the user instead of the student list presentation.

As we can see from the above example, the model handles all of the data. The view handles all of the
presentations and the controller just tells the model and view of what to do. This is the basic architecture
and working of the MVC framework.

The MVC architectural pattern allows us to adhere to the following design principles:
1. Divide and conquer. The three components can be somewhat independently designed.
2. Increase cohesion. The components have stronger layer cohesion than if the view and controller were
together in a single UI layer.
3. Reduce coupling. The communication channels between the three components are minimal and easy to

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

find.
4. Increase reuse. The view and controller normally make extensive use of reusable components for
various kinds of UI controls. The UI, however, will become application-specific, therefore it will not be
easily reusable.
5. Design for flexibility. It is usually quite easy to change the UI by changing the view, the controller, or
both.

Advantages of MVC

• Codes are easy to maintain and they can be extended easily.

• The MVC model component can be tested separately.

• The components of MVC can be developed simultaneously.

• It reduces complexity by dividing an application into three units. Model, view, and controller.

• It supports Test Driven Development (TDD).

• It works well for Web apps that are supported by large teams of web designers and developers.

• This architecture helps to test components independently as all classes and objects are independent
of each other

• Search Engine Optimization (SEO) Friendly.

Disadvantages of MVC

• It is difficult to read, change, test, and reuse this model

• It is not suitable for building small applications.

• The inefficiency of data access in view.

• The framework navigation can be complex as it introduces new layers of abstraction which requires
users to adapt to the decomposition criteria of MVC.

• Increased complexity and Inefficiency of data

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Web Browser
The web browser is an application software to explore www (World Wide Web). It provides an interface
between the server and the client and it requests to the server for web documents and services. It works
as a compiler to render HTML which is used to design a webpage. Whenever we search for anything on the
internet, the browser loads a web page written in HTML, including text, links, images, and other items such
as style sheets and JavaScript functions. Google Chrome, Microsoft Edge, Mozilla Firefox, and Safari are
examples of web browsers.

Web Server:

Web server is a program which processes the network requests of the users and serves them with files
that create web pages. This exchange takes place using Hypertext Transfer Protocol (HTTP).
Basically, web servers are computers used to store HTTP files which makes a website and when a client
requests a certain website, it delivers the requested website to the client. For example, you want to open
Facebook on your laptop and enter the URL in the search bar of google. Now, the laptop will send an HTTP
request to view the facebook webpage to another computer known as the webserver. This computer
(webserver) contains all the files (usually in HTTP format) which make up the website like text, images,
gif files, etc. After processing the request, the webserver will send the requested website-related files to
your computer and then you can reach the website.
Different websites can be stored on the same or different web servers but that doesn’t affect the actual
website that you are seeing in your computer. The web server can be any software or hardware but is
usually a software running on a computer. One web server can handle multiple users at any given time
which is a necessity otherwise there had to be a web server for each user and considering the current
world population, is nearly close to impossible. A web server is never disconnected from the internet
because if it was, then it won’t be able to receive any requests, and therefore cannot process them.

MVC Design Pattern

The Model View Controller (MVC) design pattern specifies that an application consists of a data model,
presentation information, and control information. The pattern requires that each of these be separated into
different objects.

• The MVC pattern separates the concerns of an application into three distinct components, each
responsible for a specific aspect of the application’s functionality.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

• This separation of concerns makes the application easier to maintain and extend, as changes to one
component do not require changes to the other components.

Components of the MVC Design Pattern

1. Model

The Model component in the MVC (Model-View-Controller) design pattern represents the data and business
logic of an application. It is responsible for managing the application’s data, processing business rules, and
responding to requests for information from other components, such as the View and the Controller.

2. View

Displays the data from the Model to the user and sends user inputs to the Controller. It is passive and does
not directly interact with the Model. Instead, it receives data from the Model and sends user inputs to the
Controller for processing.

3. Controller

Controller acts as an intermediary between the Model and the View. It handles user input and updates the
Model accordingly and updates the View to reflect changes in the Model. It contains application logic, such
as input validation and data transformation.

Communication between the components

This below communication flow ensures that each component is responsible for a specific aspect of the
application’s functionality, leading to a more maintainable and scalable architecture

• User Interaction with View:

o The user interacts with the View, such as clicking a button or entering text into a form.

• View Receives User Input:

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

o The View receives the user input and forwards it to the Controller.

• Controller Processes User Input:

o The Controller receives the user input from the View.

o It interprets the input, performs any necessary operations (such as updating the Model), and
decides how to respond.

• Controller Updates Model:

o The Controller updates the Model based on the user input or application logic.

• Model Notifies View of Changes:

o If the Model changes, it notifies the View.

• View Requests Data from Model:

o The View requests data from the Model to update its display.

• Controller Updates View:

o The Controller updates the View based on the changes in the Model or in response to user
input.

• View Renders Updated UI:

o The View renders the updated UI based on the changes made by the Controller.

Example of the MVC Design Pattern

Below is the code of above problem statement using MVC Design Pattern:

Let’s break down into the component wise code:

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

1. Model (Student class)

Represents the data (student’s name and roll number) and provides methods to access and modify this data.

• Java

class Student {

private String rollNo;

private String name;

public String getRollNo() {

return rollNo;

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

public void setRollNo(String rollNo) {

this.rollNo = rollNo;

public String getName() {

return name;

public void setName(String name) {

this.name = name;

2. View (StudentView class)

Represents how the data (student details) should be displayed to the user. Contains a method
(printStudentDetails) to print the student’s name and roll number.

• Java

class StudentView {

public void printStudentDetails(String studentName, String studentRollNo) {

System.out.println("Student:");

System.out.println("Name: " + studentName);

System.out.println("Roll No: " + studentRollNo);

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

3. Controller (StudentController class)

Acts as an intermediary between the Model and the View. Contains references to the Model and View objects.
Provides methods to update the Model (e.g., setStudentName, setStudentRollNo) and to update the View
(updateView).

• Java

class StudentController {

private Student model;

private StudentView view;

public StudentController(Student model, StudentView view) {

this.model = model;

this.view = view;

public void setStudentName(String name) {

model.setName(name);

public String getStudentName() {

return model.getName();

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

public void setStudentRollNo(String rollNo) {

model.setRollNo(rollNo);

public String getStudentRollNo() {

return model.getRollNo();

public void updateView() {

view.printStudentDetails(model.getName(), model.getRollNo());

Complete code for the above example

Below is the complete code for the above example:

• Java

class Student {

private String rollNo;

private String name;

public String getRollNo() {

return rollNo;

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

public void setRollNo(String rollNo) {

this.rollNo = rollNo;

public String getName() {

return name;

public void setName(String name) {

this.name = name;

class StudentView {

public void printStudentDetails(String studentName, String studentRollNo) {

System.out.println("Student:");

System.out.println("Name: " + studentName);

System.out.println("Roll No: " + studentRollNo);

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

class StudentController {

private Student model;

private StudentView view;

public StudentController(Student model, StudentView view) {

this.model = model;

this.view = view;

public void setStudentName(String name) {

model.setName(name);

public String getStudentName() {

return model.getName();

public void setStudentRollNo(String rollNo) {

model.setRollNo(rollNo);

public String getStudentRollNo() {

return model.getRollNo();

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

public void updateView() {

view.printStudentDetails(model.getName(), model.getRollNo());

public class MVCPattern {

public static void main(String[] args) {

Student model = retriveStudentFromDatabase();

StudentView view = new StudentView();

StudentController controller = new StudentController(model, view);

controller.updateView();

controller.setStudentName("Vikram Sharma");

controller.updateView();

private static Student retriveStudentFromDatabase() {

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Student student = new Student();

student.setName("Lokesh Sharma");

student.setRollNo("15UCS157");

return student;

• Output

Student:

Name: Lokesh Sharma

Roll No: 15UCS157

Student:

Name: Vikram Sharma

Roll No: 15UCS157

FRAMEWORK
Advantages of frameworks
• Speed of development: they provide structures and templates to organize the application’s code
and files, as well as common features. This allows developers to save time by avoiding reinventing
the wheel and focusing on the business logic of their application.

• Error reduction: They often include security, user input validation and error handling features. This
reduces the risk of errors and security vulnerabilities in the application.

• Scalability: They allow you to create scalable and modular applications by using reusable
components and a well-structured architecture.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

• Documentation: They are often well documented, making it easier for developers to learn and
understand.

Framework Limitations
• While frameworks offer many advantages in IT development, they also have some disadvantages that
include the following:

• Limited creativity: Frameworks have strict rules and conventions that must be followed in order
for the code to work properly. This can limit the developer’s creativity and make it difficult to
implement custom features.

• Application size: They often include features and libraries that are not necessary for the application
being developed, which can lead to an increase in the size of the application. This can slow down the
time it takes to download and run the application.

• Compatibility: Some frameworks may not be compatible with all platforms and environments and
this can potentially limit deployment options. In addition, various frameworks may also require
specific versions of programming languages or third-party software, which can make installation and
operation difficult.

• Learning curve: Learning a new framework can take time and may require additional training for
developers. It can also make the recruitment process more difficult, as it can often be challenging to
find developers that have prior experience within a specific framework.

Most Popular Frameworks in 2023


In 2023, the most used frameworks will depend on the most popular technologies and programming
languages available. However, here is a list of the most popular frameworks from 2021, widely expected to
continue their popularity well into 2023:

• ReactJS: JavaScript framework for creating dynamic and responsive user interfaces (UIs).

• Angular: JavaScript framework developed by Google for creating web and mobile applications.

• Vue.js: JavaScript framework that allows you to create interactive and responsive web applications
with a simple syntax.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

• Django: Python framework for rapid development of web applications, based on the Model-View-
Controller (MVC) principle.

• Ruby on Rails: Ruby framework for rapid development of web applications, also using the MVC
model.

• Laravel: PHP framework for developing modern and elegant web applications.

• Spring: Java framework that provides a complete platform for enterprise application development.

• Express: JavaScript framework for building web applications in Node.js.

• Flask: Python framework for developing lightweight and simple web applications.

• ASP.NET: Microsoft framework for developing web and mobile applications, based on the .NET
framework.

The role of Express – Angular – Node – Mongo DB – React

MongoDB—a NoSQL document database


* Express(.js) — Node.js web framework
* React(.js) — a client-side JavaScript framework
* Node(.js) — the premier JavaScript web server

Node.js

(Node js) is an open-source and cross-platform JavaScript runtime environment. It runs on Chrome’s V8
JavaScript engine. It allows developers to run JavaScript code on the server. Node.js enables developers to
get into the server-side world.

Node.js is an open source server environment that uses JavaScript on server. A


Node.js application runs within a single process, without generating a new thread for each
request. Node.js includes asynchronous I/O primitives as a part of its standard library, which
prevents JavaScript code from blocking and, in general, libraries in Node.js are developed using
non-blocking paradigms. This makes blocking behaviour the exception instead of the rule.

It is developed by Ryan Dahl in the year 2009 and v22.4.1 is the latest version of Node.js. Because
it is cross-platform one can easily run on Windows, Linux, Unix, macOS and more.

Features of Node.js:

1. JavaScript Everywhere: Node.js enables developers to use JavaScript across the entire
stack, from front-end to back-end. This consistency simplifies development and reduces
context switching.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

2. Asynchronous Programming Model: Node.js uses an event-driven, non-blocking


(asynchronous) I/O model. This allows handling multiple requests simultaneously without
blocking the execution of other tasks. As a result, Node.js applications are highly responsive
and efficient.

3. Fast Execution: Node.js leverages the V8 engine, developed by Google, which compiles and
executes JavaScript at lightning speeds. This performance advantage makes it suitable for
real-time applications and microservices.

4. Large and Active Community: Node.js has a vibrant community of developers, libraries,
and tools. You’ll find extensive resources, tutorials, and support to enhance your learning
experience.

5. Scalability: Node.js is lightweight and scalable, making it an excellent choice for building
real-time applications, RESTful APIs, and microservices.

6. Cross-Platform Compatibility: Node.js runs on Windows, Linux, Unix, macOS, and


more. This flexibility allows developers to write code once and deploy it anywhere.

Node.js Advantages

• Easy Scalability: Node.js compiles and executes JavaScript at lightning speeds, making it
highly scalable.

• Real-time Web Apps: Node.js enables real-time communication for chat, gaming, social
media updates, and more.

• Microservices: Node.js is lightweight and ideal for microservice architectures.

• JavaScript Everywhere: Learn JavaScript once, and you can use it both for front-end and
back-end development.

• Efficient Data Streaming: Node.js efficiently handles I/O processes like media transcoding
during uploads.

• Event-Driven Architecture: Unlike traditional servers, Node.js handles concurrent


requests effectively.

• Strong Community Support: Node.js has an independent community backing its


development.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Express.js

• Express.js is a fast, flexible and minimalist web framework for Node.js. It’s effectively a tool that simplifies
building web applications and APIs using JavaScript on the server side. Express is an open-source that is
developed and maintained by the Node.js foundation.
• Express.js offers a robust set of features that enhance your productivity and streamline your web
application. It makes it easier to organize your application’s functionality with middleware and routing. It
adds helpful utilities to Node HTTP objects and facilitates the rendering of dynamic HTTP objects.

Features of Express

1. Middleware and Routing: Define clear pathways (routes) within your application to handle incoming HTTP
requests (GET, POST, PUT, DELETE) with ease. Implement reusable functions (middleware) to intercept
requests and create responses, adding functionalities like authentication, logging, and data parsing.

2. Minimalistic Design: Express.js follows a simple and minimalistic design philosophy. This simplicity allows
you to quickly set up a server, define routes, and handle HTTP requests efficiently. It’s an excellent choice
for building web applications without unnecessary complexity.

3. Flexibility and Customization: Express.js doesn’t impose a strict application architecture. You can
structure your code according to your preferences. Whether you’re building a RESTful API or a full -fledged
web app, Express.js adapts to your needs.

4. Templating Power: Incorporate templating engines like Jade or EJS to generate dynamic HTML content,
enhancing user experience.

5. Static File Serving: Effortlessly serve static files like images, CSS, and JavaScript from a designated directory
within your application.

6. Node.js Integration: Express.js seamlessly integrates with the core functionalities of Node.js, allowing you
to harness the power of asynchronous programming and event-driven architecture.

MongoDB

MongoDB is a document database. It stores data in a type of JSON format called BSON.

A record in MongoDB is a document, which is a data structure composed of key value pairs similar to the structure of
JSON objects.

Records in a MongoDB database are called documents, and the field values may include numbers, strings, booleans,
arrays, or even nested documents.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

title: "Post Title 1",

body: "Body of post.",

category: "News",

likes: 1,

tags: ["news", "events"],

date: Date()

MongoDB use:

Document Oriented Storage:

Data is stored in the form of JSON style documents.

• Index on any attribute

• Replication and high availability

• Auto-sharding

• Rich queries

• Fast in-place updates

• Professional support by MongoDB

Where to use Big Data

• Content Management and Delivery

• Mobile and Social Infrastructure

User Data Management

• Data Hub

React

React is a JavaScript library for building user interfaces.

React is used to build single-page applications.

React allows us to create reusable UI components.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

It is a powerful JavaScript library for building dynamic and interactive user interfaces (UIs). It is developed by
Facebook. React is known for its component-based architecture which allows you to create reusable UI elements,
making complex web applications easier to manage and maintain. React is used to build single-page applications.

React Advantages

• Composable: We can divide these codes and put them in custom components. Then we can utilize
those components and integrate them into one place.

• Declarative: In ReactJS, the DOM is declarative. We can make interactive UIs by changing the state
of the component and ReactJS takes care of updating the DOM according to it.

• SEO Friendly: ReactJS affects the SEO by giving you a SPA (Single Page Application) which requires
Javascript to show the content on the page which can be rendered and indexed.

• Community: ReactJS has a huge community because of its demand each company wants to work
with ReactJS. Companies like Meta, Netflix, etc built on ReactJS.

• Easy to learn: HTML-like syntax of JSX makes you comfortable with the codes of React, it only
requires a basic knowledge of HTML, CSS, and JS fundamentals to start working with it.

Debugging is Easy: The debugging of ReactJS is unidirectional which means while designing any
app using ReactJS the child components are nested within parent components. So, the data flow is
in a single direction it gets more easier to debug errors.

Unit II: Node JS

Basics of Node JS – Installation – Working with Node packages – Using Node package manager –
Creating a simple Node.js application – Using Events – Listeners –Timers – Callbacks – Handling
Data I/O – Implementing HTTP services in Node.js

Basics of Node JS

➢ Node.js is an open source server environment.


➢ Node.js allows you to run JavaScript on the server.
➢ Node.js is an open-source cross-platform runtime environment that allows you to use JavaScript to
develop server-side applications.
➢ Every web browser has a JavaScript engine that compiles JavaScript code to machine code. For
example, Firefox uses SpiderMonkey, and Google Chrome uses V8.
➢ Node.js can generate dynamic page content
➢ Node.js can create, open, read, write, delete, and close files on the server

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

➢ Node.js can collect form data


➢ Node.js can add, delete, modify data in your database

Node.js file

• Node.js files contain tasks that will be executed on certain events


• A typical event is someone trying to access a port on the server
• Node.js files must be initiated on the server before having any effect
• Node.js files have extension ".js"

Advantages of Node.js

1. Node.js is an open-source framework under MIT license. (MIT license is a free software license
originating at the Massachusetts Institute of Technology (MIT).)
2. Uses JavaScript to build entire server side application.
3. Lightweight framework that includes bare minimum modules. Other modules can be included as per
the need of an application.
4. Asynchronous by default. So it performs faster than other frameworks.
5. Cross-platform framework that runs on Windows, MAC or Linux.

The following picture illustrates the Node.js system:

Node.js uses the single-threaded, non-blocking, and event-driven execution model, which is similar to the
execution model of JavaScript in the web browser.

Node.js is single-threaded

➢ Node.js is single-threaded, meaning each process runs only one thread of execution.
➢ The single-threaded execution model allows Node.js to handle more concurrent requests easily via
the event loop. As a result, Node.js applications typically consume less memory compared to others.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Node.js uses Non-blocking I/O

➢ I/O stands for input/output, which can involve disk access, a network request, or a database
connection. I/O requests are expensive and slow, leading to the potential blocking of other
operations
➢ Node.js addresses blocking I/O issues by utilizing non-blocking I/O operations.
➢ In the non-blocking model, you can initiate a request while doing other tasks. Once the request is
completed, a callback function is executed to handle the result.

Node.js is event-driven

Node.js uses event-driven architecture, meaning that Node.js waits for events to happen and responds to
them when they occur, instead of following a linear, top-to-bottom execution model.

The following describes how Node.js event-driven architecture works:

1. An event, such as a client sending a request to a server, triggers an event handler function.
2. Node.js then adds the event handler (a callback function) to an Event Queue.
3. The Event Loop, which is a continuous loop running in Node.js, checks the Event Queue and
processes the events one by one. It executes the corresponding event handler for each event.
4. While the Event Loop is processing these events, Node.js can continue handling other tasks, such as
listening for new events.

Install Node.js

➢ Download Node.js
➢ To download Node.js, you go to the download page on the Node.js website.
➢ Node.js supports multiple platforms, including Windows, macOS, and Linux. You need
to choose the installer suitable for your platform.
➢ Node.js has two actively supported releases:

• Long-Term Support (LTS)


• Current

➢ If you develop real-use applications, you should only use the LTS releases. The LTS release
is recommended for most users.
➢ Install Node.js on Windows
➢ To install Node.js on Windows, double-click the installer file you have downloaded to
launch the setup wizard.
➢ First, the setup wizard will compute space requirements:

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

After completed, you click the Next button to go to the next step:

Second, accept the end-user license agreement and click the Next button.

Third, select a folder to install Node.js and click the Next button. It defaults to C:\Program
Files\nodejs\.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Fourth, select the features you want to install and click the Next button. If you leave the default
options, the setup wizard will install Node.js runtime, npm package manager, and online
documentation shortcuts. It also adds the C:\Program Files\nodejs\ folder to the PATH:

Fifth, select the checkbox to install the necessary tools, including Chocolatey. If you don’t know
what it is, leave it unchecked and click the Next button.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Sixth, click the Install button to begin the installation.

Seventh, you need to wait for the setup wizard to install Node.js:

Eight, click the Finish button to exit the setup wizard.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

To confirm the installation, you can open Command Prompt or Windows Terminal and type the
following command:

node -v

It will show the installed version of Node.js. If you type node, it will launch a REPL and put you in
an interactive JavaScript environment. In this interactive mode, you can execute any valid
JavaScript code.

> 'hello'.length
5
> 'hello'.toUpperCase();
'HELLO'
>Code language: JavaScript (javascript)

To escape the interactive mode, you type the .exit command:

>.exitCode language: CSS (css)

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

First Node.js Script


➢ Create and run your very first Node.js app.
➢ Creating a Script Node.js scripts are created with the js file extension.
➢ Node.js is not a programming language.
➢ All the code in this course is JavaScript code, which is why the js extension is used. Below is an
example script stored in a file named index.js. console.log('Hello Node.js!')
➢ Running a Script You can run a Node.js script using the node command.
➢ Open up a new terminal window and navigate to the directory where the script lives. From the
terminal, you can use the node command to provide the path to the script that should run. You can
see an example of this command in the terminal below.
➢ $ node index.js Hello Node.js!
➢ When a Node.js script calls console.log, the logged values will show up in the terminal. This is a way
to get output from your Node.js application

Working with Node packages - Node Package Manager

The crown jewels of Node.js are its packages.Node.js has a package manager. The abbreviation and the
command for the CLI is npm (Node Package Manager).

NPM is a gigantic network of development tools that you can download for free for your application. If you
work with Node.js, you will always use modules from other developers.

Advantages:

• Time saving, because you don’t have to program everything yourself


• Security vulnerabilities are (usually) closed promptly and you can update such a dependency with a
command
• Standardization ensures easier handling between multiple developers
• Good documentation on existing interfaces
Package installation

In this example we download the module express. This works with the following command:

npm install express

You can also install modules globally, which means that it applies to your entire computer and is not only
available in one project. To do this, simply append the -g parameter.

npm install -g express

Some modules are also only required for development and are later superfluous in productive use. If you
use the --save-dev parameter, these are created as dev dependencies – i.e. only required for development.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

npm install --save-dev dotenv

When you install a module, a corresponding entry is created in the package.json and the
folder node_modules is created automatically. This contains all installed modules and can be ignored by you
for now.

Integration in source code

We can include a module in the code with the require() function:

const express = require('express');


Update packages

To take advantage of the already mentioned regular (security) updates you can always keep your packages
up to date. You can simply use this command:

npm update

Create first Node.js App

Create project folder

The time has come, we are finally starting to program. To create a Node.js project, we simply create a
normal folder in any directory. In it we create an index.js, which is our start file.

Now we need to say in this folder that we want to make it a Node.js app, we do that via this command in the
CLI:

npm init

We will be asked for several parameters like package name, version and description. You can fill in these
fields or like me just leave them on default settings. We can always change these settings later in
the package.json.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Write & execute program code

Now we can write the first JavaScript code in our index.js. Of course we can’t do without it and have to
output “Hello World”.

// index.js
console.log('Hello World');

The first code is written and we want to start our program. We do that with this command:

node index.js

We get the following result:

Hello World

Using Node package manager

NPM (Node Package Manager) is the default package manager for Node and is written entirely
in JavaScript.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Events

➢ Node.js events module provides you with EventEmitter class that allows you to manage events in
the node application.
➢ Use the on() method of the EventEmitter object to register an event handler for an event.
➢ Use the emit() method of the EventEmitter object to emit an event.
➢ Node.js is event-driven. It relies on the events core module to achieve the event-driven architecture.
➢ In the event-driven model, an EventEmitter object raises an event that causes the previously
attached listeners of the event executed.
➢ An EventEmitter object has two main functions:
➢ Emit a named event.
➢ Attach and detach one or more event listeners to the named event.
➢ In Node.js, many core modules inherit the EventEmitter class including http module.

The following example illustrates how to use the events module.

First, include the events module using the require() function:

const EventEmitter = require('events');Code language: JavaScript (javascript)

The EventEmitter is a class, so by convention its name is in the camel case.

Second, create a new instance of the EventEmitter class:

const emitter = new EventEmitter();


Code language: JavaScript (javascript)

Third, attach one or more event handlers to the event by using the on() method:

emitter.on('saved', ) => {
console.log(`A saved event occurred.`);
});
Code language: JavaScript (javascript)

In this example, the event name is saved and the event handler is a callback. When the saved event occurs,
the callback is invoked automatically.

Finally, emit the saved event using the emit() method of the EventEmitter object:

emitter.emit('saved');Code language: JavaScript (javascript)

Put it all together:

const EventEmitter = require('events');

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

const emitter = new EventEmitter();

emitter.on('saved', () => {
console.log(`A saved event occurred.`);
});

emitter.emit('saved');Code language: JavaScript (javascript)

Output:

A saved event occurred.


Code language: JavaScript (javascript)

Emit an event with arguments

When raising an event, you may want to pass some data to the event listener e.g., the object that has been
saved when the saved event occurs. In this case, you can pass the data into the emit() method as the second
argument. For example:

const EventEmitter = require('events');

const emitter = new EventEmitter();

emitter.on('saved', (arg) => {


console.log(`A saved event occurred: name: ${arg.name}, id: ${arg.id}`);
});

emitter.emit('saved', {
id: 1,
name: 'John Doe'
});Code language: JavaScript (javascript)

Output:

A saved event occurred, name: John Doe, id: 1Code language: JavaScript (javascript)

In this example, when we emit the saved event, we pass an object that has two properties id and name to
the emit() method. And when the event occurs, we show the object properties to the console.

Detach an event listener

To detach an event listener from an event, you use the off() method of the EventEmitter object. For
example:

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

const EventEmitter = require('events');

const emitter = new EventEmitter();

// declare the event handler


function log(arg) {
console.log(`A saved event occurred, name: ${arg.name}, id: ${arg.id}`);
}

// attach the event listener to the saved event


emitter.on('saved', log);

// emit the saved event


emitter.emit('saved', {
id: 1,
name: 'John Doe'
});

// remove the event listener


emitter.off('saved', log);

// no effect
emitter.emit('saved', {
id: 2,
name: 'Jane Doe'
});Code language: JavaScript (javascript)

Output:

A saved event occurred, name: John Doe, id: 1

In this example, after detaching the event listener from the event. The event listener is not called when
the saved event is emitted.

Extend the EventEmitter class

The following example illustrates how to extend the EventEmitter class:

const EventEmitter = require('events');

class Stock extends EventEmitter {


constructor(symbol, price) {
super();
this._symbol = symbol;
this._price = price;
}

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

set price(newPrice) {
if (newPrice !== this._price) {
this.emit('PriceChanged', {
symbol: this._symbol,
oldPrice: this._price,
newPrice: newPrice,
adjustment: ((newPrice - this._price) * 100 / this._price).toFixed(2)
});
}
}
get price() {
return this._price;
}
get symbol() {
return this._symbol;
}
}
Code language: JavaScript (javascript)

How it works.

• The Stock class extends the EventEmitter class. It has two properties stock symbol and price.
• When the price changes, the price setter emits the PriceChanged event with an object.

The following illustrates how to use the Stock class:

const stock = new Stock('AAPL', 700);

stock.on('PriceChanged', (arg) => {


console.log(`The price of the stock ${arg.symbol} has changed ${arg.adjustment}%`);
})

stock.price = 720;
Code language: JavaScript (javascript)

Output:

The price of the stock AAPL has changed 2.86%

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Node JS Events

Node.js is an asynchronous, event-driven, non-blocking I/O application. It is built with a V8 engine along
with other node core components like Libuv. Libuv library developed in C++ provides event loop
mechanism and I/O API for concurrent operations. Thanks to this multithreading language which helps in
handling concurrent tasks. This mechanism makes Node.js an asynchronous event-based platform in which
everything is a reaction to an event.

Above diagram gives a glimpse of how Node.Js works and will use it to understand the event loop better.
Node.js is a single thread application and all the blocking calls are stored in the call stack and once they are
processed they are removed from the call stack. Let's take an example:

• Console.log(‘hi’); //1
• Console.log(‘welcome’); //2
• Console.log(‘Happy learning’); //3

When we run a node, line 1 is moved to call stack and executed and then followed up by line 2 and then line
3. A similar thing can be observed if an error is raised then from the error stack, we can know the order of
execution.

Node.js provides timer functions globally. We can work with Node.js asynchronously is through using the
timing functions. The timing functions setTimeout, clearTimeout, setInterval, and clearInterval work the
same way they do in the browser and are available to you globally.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

So whenever asynchronous calls are observed during execution, those are moved to API block and the call
stack is continued. When the asynchronous operation is done say setTimeout, the execution of call-backs is
done by the event loop i.e. it moved the callback of setTimeout to Event Queue. And event loop will be
keeping monitoring the call stack to get empty. And once it is empty then call back is pushed to call stack
and executed.

Note: Libuv library basically creates a thread pool to offload the asynchronous work. If the operating system
provides an interface for I/O tasks then this is used instead of working with threads.

Below are the examples of global timer functions:

App1: Simple example using setTimeout with a callback to log in console.

• Create a timer.js file


• Add a variable say ‘waitTime’ with 3 seconds i.e. 3000 in milliseconds.
• Let's use setTimeout to wait for 3 seconds and console.log in a callback which gets executed after a
delay.

var waitTime = 3000;

console.log(“wait for it”);

setTimeout(() => console.log(“done”), waitTime);

• This setTimeout is going to make application wait for 3 seconds and then it will invoke the callback.
• Let's go to terminal and run node timer.js to observe that application waited 3 seconds after logging
the first message and done.
• So during these three seconds while we're waiting for our application is going to be running.

App2: Extending above example with usage of setInterval along wit

• Let's extend the above one with setInterval function.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

• Add a couple more variables say currentTime which we can initialize it to 0 milliseconds and
waitInterval with 500 (half a second).
• WaitInterval is used to fire the callback of setInterval every half second.

var waitTime = 3000;

var currentTime = 0;

var waitInterval = 500;

console.log(“wait for it”);

setTimeout(() => console.log(“done”), waitTime);

• setTimeout waits for a delay once and then invokes the callback function, where as setInterval will
fire the callback function over and over again on an interval time. Lets add a setInterval.

var waitTime = 3000;

var currentTime = 0;

var waitInterval = 500;

console.log(“wait for it”);

setInterval(() => {

currentTime =+ waitInterval;

console.log(`waiting ${currentTime/1000} seconds…`); // to display in seconds

}, waitInterval);

setTimeout(() => console.log(“done”), waitTime);

• run node timer.js

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

• We will observe that waiting is keeping incrementing by half second and once we reach 3 seconds,
done is displayed.
• Notice that after the done log we are still going. That interval will not stop.
• Lets add an exit criteria after 3 seconds and to stop application hit control C.
• setInterval return has to be set in a variable. And call clearInterval with argument as this variable
inside setTimeOut.

var waitTime = 3000;

var currentTime = 0;

var waitInterval = 500;

console.log(“wait for it”);

var interval = setInterval(() => {

currentTime =+ waitInterval;

console.log(`waiting ${currentTime/1000} seconds…`);

}, waitInterval);

setTimeout(() => {

clearInterval(interval);

console.log(“done”)

}, waitTime);

• Now on running node timer.js, we will see that post 3 seconds the application is stopped.

App3:

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Let's modify this code to display the time waiting in a percentage and also control the standard output, so
that we overwrite the last line, meaning that we can see a percentage number grow.

• Lets add a function for writing the percentage writeWaitingPercent.

function writeWaitingPercent(percent) {

process.stdout.clearLine(); // clears the previous line

process.stdout.cursorTo(0); // moves the cursor to first line

process.stdout.write(`waiting ... ${percent}%`); // add the text to first line

• Lets add a variable to percentWaited and set to 0


• Call the writeWaitingPercent with percentWaited at the end of the file
• Also updated the setTimeOut to call this writeWaitingPercent with 100 in callback.

setTimeout(() => {

clearInterval(interval);

writeWaitingPercent(100);

console.log(“done”)

}, waitTime);

• In setInterval callback also, lets call the writeWaitingPercent based on the calculated percentWaited
based on currentTime.

var interval = setInterval(() => {

currentTime =+ waitInterval;

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

percentWaited = Math.floor((currentTime/waitTime) * 100);

writeWaitingPercent(percentWaited);

}, waitInterval);

• Lets run node timer in terminal observe the percentage increase.


• To observe a slow increase we can reduce the waitInterval to much lesser value say 50
milliseconds.

varwaitTime = 3000;

varcurrentTime = 0;

varwaitInterval = 500;

varpercentWaited = 0;

functionwriteWaitingPercent(percent) {

process.stdout.clearLine();

process.stdout.cursorTo(0);

process.stdout.write(`waiting ... ${percent}%`);

varinterval = setInterval(function() {

currentTime += waitInterval;

percentWaited = Math.floor((currentTime/waitTime) * 100);

writeWaitingPercent(percentWaited);

}, waitInterval);

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

setTimeout(function() {

clearInterval(interval);

writeWaitingPercent(100);

console.log("\n\n done \n\n");

}, waitTime);

writeWaitingPercent(percentWaited);

Output:

node timers

waiting ... 100%

done

EventEmitter

EventEmitter is another core module which comes along with Node.js. It helps in build a pub sub pattern
i.e. to emit and listen to custom events. Lets use this module and understand with the help of an example.

App1:

• Lets create an event.js file and create a new variable events to import this module.
• const events = require(“events”);
• Create an instance of the event emitter.
• const emitter = new events.EventEmitter();
• We can emit an event using this emitter and event name be ‘CustomEvent’ and from node.
• emitter.emit(“CustomEvent”, “Raising a custom event”, “node”);
• Now, we need to capture this event and handle which can be done using the same emitter.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

• emitter.on(“CustomEvent”, (msg, user) => console.log(`${user}:${msg}`));


• This has to be declared before emitting the event.
• Open the terminal and run node events
• Output has to be Raising a custom event.

App2:

One other important about these events is that they are asynchronous and raised when they happen. Let
take another example.

• Taking the above example.


• Let's listen to the user inputs and react to it. process.stdin i.e. readable stream helps in listening to
data input.
• process.stdin.on(“data”, data => { });
• Remove the code from previous example code related to emitting of custom event.
• In the callback of it, let's emit the customEvent with the data entered by the user.
• process.stdin.on(“data”, data => {

const input = data.toString().trim();

emitter.emit(“customEvent”, input, “terminal”);

});

1. If user enters ‘exit’, then lets exit the process by emiting that user has exited.
2. process.stdin.on(“data”, data => {

const input = data.toString().trim();

if (input === ‘exit’) {

emitter.emit(“customEvent”, “user has exited”, “terminal”);

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

process.exit();

emitter.emit(“customEvent”, input, “terminal”);

});

• Remove the code from previous example code related to emitting of custom event.
• run node events in the terminal
• Now the terminal is waiting for user inputs and not exiting. This is because the process.stdin is
waiting for the data.
• Enter the text says ‘hello event emitter’
• The output should be terminal: hello event emitter.
• On entering the ‘exit’ text it should exit the process.

App3:

Let's create an object and add event handling mechanism to that object with the help of eventEmitter.

• Create a customEventObject.js file


• Import the event emitter using the required module.
• const eventEmitter = require(“events”).EventEmitter;
• Create a person object.
• var person = (name) => {

this.name = name;

• We want the eventEmitter to be inherited by this person object. We can actually use the util module
that is part of node.js for inheriting.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

• At the top of the file, get util using require a module.


• const util = require(“util”);
• Util module has a function called inherits where we can add an object to a prototype of another
existing object and that’s how inheritance works in JS.
• util.inherits(person, EventEmitter);
• Above line has just had person object inherit the eventEmitter and thereby providing the
functionality to emit and listen to events.
• Let's create an instance of this person object
• var employee = new Person(‘SomePerson’);
• Now, this employee can listen to any events. So let's add an event handler on this object.
• employee.on(‘programmer’, (msg) => {

console.log(`${this.name}`: ${msg});

});

• Let's raise an event using emit.


• employee.emit(‘programmer’, ‘You are a good learner’);
• Save and run the node customEventObject.js in terminal
• The output should be, SomePerson: You are a good learner.

varEventEmitter = require('events').EventEmitter;

varutil = require('util');

varPerson = function(name) {

this.name = name;

};

util.inherits(Person, EventEmitter);

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

varemployee = newPerson("SomePerson");

employee.on('programmer', function(msg) {

console.log(`${this.name}: ${msg}`);

});

employee.emit('programmer', "You are a good learner.");

Output:

SomePerson: You are a good learner.

4-A Getting Started with Node.js

6-A Handling Data

event loop in Node.js

The Node.js event loop is a continuously running, semi-infinite loop. It runs for as long as there is a pending
asynchronous operation. Starting a Node.js process using the node command executes your JavaScript code
and initializes the event loop. If Node.js encounters an asynchronous operation such as timers, files, and
network I/O while executing a script, it offloads the operation to the native system or the thread pool.

Most I/O operations, such as reading and writing to file, file encryption and decryption, and networking,
are time-consuming and computationally expensive. Therefore, to avoid blocking the main thread, Node.js
offloads these operations to the native system. There, the Node process is running, so the system handles
these operations in parallel.

Most modern operating system kernels are multi-threaded by design. Therefore, an operating system can
handle multiple operations concurrently and notifies Node.js when those operations are complete. The
event loop is responsible for executing your asynchronous API callbacks. It has six major phases:

1. Timers phase for handling setTimeout and setInterval


2. Pending callbacks phase for executing deferred callbacks
3. Idle, Prepare phase that the event loop uses for internal housekeeping
4. Poll phase for polling and handling events such as file and network I/O
5. Check phase for executing setImmediate callbacks
6. Close phase for handling certain close events
Though the above list is linear, the event loop is cyclical and iterative, as in the diagram below:

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

After the event loop’s last phase, the next iteration of the event loop starts if there are still pending events
or asynchronous operations. Otherwise, it exits, and the Node.js process ends.

We will explore each phase of the event loop in detail in the following sections. Before that, let’s explore the
“next tick” and microtask queues that appear at the center of the event loop in the above diagram.
Technically, they are not part of the event loop.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Timers phase
Like the browser, Node.js has the timers API for scheduling functions that will execute in the future. The
timers API in Node.js is similar to that in the browser. However, there are some slight implementation
differences.
The timers API consists of the setTimeout, setInterval, and setImmediate functions. All three timers are
asynchronous. The timers phase of the event loop is only responsible for
handling setTimeout and setInterval.

On the other hand, the check phase is responsible for the setImmediate function. We will explore the check
phase later. Both setTimeout and setInterval have the function signature below:

setTimeout(callback[, delay[, ...args]])


setInterval(callback[, delay[, ...args]])
o callback is the function to invoke when the timer expires
o delay is the number of milliseconds to wait before invoking callback. It defaults to one millisecond
o args are the optional arguments passed to callback
With setTimeout, callback is invoked once when delay elapses. On the other
hand, setInterval schedules callback to run every delay milliseconds.

The diagram below shows the event loop after removing all the phases except the timers phase:

For simplicity, let’s take three scheduled setTimeout that expire concurrently. The steps below describe
what happens when the event loop enters the timer phase:

1. The three expired timers are added to the timers queue


2. The event loop executes the first setTimeout callback. If “next ticks” or microtasks are generated
while executing the first callback, they are added to their respective queue
3. When the first setTimeout callback returns, the “next tick” queue is processed. If more “next ticks”
are generated while processing the “next tick” queue, they’re added to the back of the “next tick”
queue and processed immediately. If microtasks are generated, they are added to the microtask
queue
4. When the “next tick” queue is empty, the microtask queue is processed. If more “micro tasks” are
generated, they’re added at the back of the microtask queue and processed immediately
5. If both the “next tick” queue and microtask queue are empty, the event loop will execute the second
callback in the timers queue. Steps two-four are repeated for the second and third callbacks
6. After executing all the expired timer callbacks or the maximum number of callbacks, the event loop
goes to the next phase
In the steps above, we used a queue of three expired timers. However, that will not always be the case in
practice. The event loop will process the timers queue until it is empty or the maximum number of
callbacks is reached before moving to the next phase.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

The event loop is blocked when executing JavaScript callbacks. If a callback takes long to process, the event
loop will wait until it returns. Because Node.js mostly runs on the server side, blocking the event loop will
lead to performance issues.

Similarly, the delay argument you pass to the timer functions is not always the exact waiting time before
executing the setTimeout or setInterval callback. It is the minimum waiting time. The duration it takes
depends on how busy the event loop is, and the system timer used.

Pending callbacks
During the polling phase, which we will cover shortly, the event loop polls for events such as file and
network I/O operations. The event loop processes some of the polled events within the poll phase and
defers specific events to the pending phase in the next iteration of the event loop.

In the pending phase, the event loop adds the deferred events to the pending callbacks queue and executes
them. Events processed in the pending callbacks phase include certain TCP socket errors emitted by the
system. For example, some operating systems defer the handling of the ECONNREFUSED error events to
this phase.

Idle, prepare
The event loop uses the idle, prepare phase for internal housekeeping operations. It doesn’t have a direct
effect on the Node.js code you write. Though we won’t explore it in detail, it is necessary to know that it
exists.
Poll phase
The poll phase has two functions. The first is to process the events in the poll queue and execute their
callbacks. The second function is to determine how long to block the event loop and poll for I/O events.

When the event loop enters the poll phase, it queues pending I/O events and executes them until the queue
is empty or a system-dependent limit is reached. In between the execution of the JavaScript callbacks, the
“next tick” and microtask queues are drained, like in the other phases.

The difference between the poll phase and other phases is that the event loop will sometimes block the
event loop for a duration and poll for I/O events until the timeout elapses, or after reaching the maximum
callback limit.

The event loop considers several factors when deciding whether to block the event loop and for how long
to block it. Some of these factors include the availability of pending I/O events and the other phases of the
event loop, such as the timers phase:

Check
The event loop executes the setImmediate callback in the check phase immediately after I/O
events. setImmediate has the function signature below:
setImmediate(callback[, ...args])
o callback is the function to invoke

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

o args are optional arguments passed to callback


The event loop executes multiple setImmediate callbacks in the order in which they are created. In the
example below, the event loop will execute the fs.readFile callback in the poll phase because it’s an I/O
operation. After that, it executes the setImmediate callbacks immediately in the check phase within the
same iteration of the event loop. On the other hand, it processes the setTimeout in the timers phase in the
next iteration of the event loop.

When you invoke the setImmediate function from an I/O callback like in the example below, the event loop
will guarantee it will run in the check phase within the same iteration of the event loop:

const fs = require("fs");

let counter = 0;

fs.readFile("path/to/file", { encoding: "utf8" }, () => {


console.log(`Inside I/O, counter = ${++counter}`);

setImmediate(() => {
console.log(`setImmediate 1 from I/O callback, counter = ${++counter}`);
});

setTimeout(() => {
console.log(`setTimeout from I/O callback, counter = ${++counter}`);
}, 0);

setImmediate(() => {
console.log(`setImmediate 2 from I/O callback, counter = ${++counter}`);
});
});
Any microtasks and “next ticks” generated from the setImmediate callbacks in the check phase are added to
the microtask queue and “next tick” queue respectively and drained immediately like in the other phases.
Close callbacks
This close phase is where Node.js executes callbacks to close events and winds down a given event loop
iteration. When a socket closes, the event loop will process the close event in this phase. If “next ticks” and
microtasks are generated in this phase, they are processed like in the other phases of the event loop.

It is worth emphasizing that you can terminate the event loop at any phase by invoking
the process.exit method. The Node.js process will exit, and the event loop will ignore pending asynchronous
operations.

The Node.js event loop in practice

As hinted above, it is important to understand the Node.js event loop in order to write performant, non-
blocking asynchronous code. Using the asynchronous APIs in Node.js will run your code in parallel, but
your JavaScript callback will always run on a single thread.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Therefore, you can unintentionally block the event loop while executing the JavaScript callback. Because
Node.js is a server-side language, blocking the event loop will make your server slow and unresponsive,
reducing your throughput.

In the example below, I am deliberately running a while loop for about a minute to simulate a long-running
operation. When you hit the /blocking endpoint, the event loop will execute the app.get callback in the poll
phase of the event loop:

const longRunningOperation = (duration = 1 * 60 * 1000) => {


const start = Date.now();
while (Date.now() - start < duration) {}
};

app.get("/blocking", (req, res) => {


longRunningOperation();
res.send({ message: "blocking route" });
});

app.get("/non-blocking", (req, res) => {


res.send({ message: "non blocking route" });
});
Because the callback is executing a time-consuming operation, the event loop is blocked for the duration
the task is running. Any request to the /non-blocking route will also wait for the event loop to first unblock.
As a result, your application will become unresponsive. Your requests from the frontend will become slow
and eventually timeout. To perform such CPU-intensive operations, you can take advantage of the worker
thread.

Similarly, do not use the synchronous APIs for the following modules on the server side because they can
potentially block the event loop:

o crypto
o zlib
o fs
o child_process
will exit immediately after the listener function returns.

Handling Data

Node.js provides a default writable stream and readable stream. Process.stdout and Process.stdIn are
writable and readable streams respectively.

Let’s understand this with an example.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Standard Input and Standard Output:

App1: Let's create an example with standard input and standard output

• Let’s create a file name stdInOut.js and an array with a list of questions.

const questions = [

"What is your name?",

"What would you rather be doing?",

"What is your preferred programming language?"

];

• Create an arrow function which takes an index and writes the question out

const ask = (i = 0) => {

process.stdout.write(`\n\n\n ${questions[i]}`);

process.stdout.write(` > `);

};

• Lets call ask method

ask();

• Create an another array to collect the answers to respective questions

const answers = [];

• We can listen to input by subscribing to ‘data’ event on standard input. All events are subscribed by
calling ‘on’ method with first argument as event name and second argument as callback.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

process.stdin.on("data", data => { });

• In the callback, we will push the data to answers array.

process.stdin.on("data", data => {

answers.push(data.toString().trim());

});

• Lets keep asking one question after every question till all are answered. And once done, we exit.

process.stdin.on("data", data => {

answers.push(data.toString().trim());

if (answers.length < questions.length) {

ask(answers.length);

} else {

process.exit();

});

• Lets add one more event handler on exit to acknowledge that all answers are captured.

process.on("exit", () => {

const [name, activity, lang] = answers;

process.stdout.write.log(`Thank you for your anwsers. Go ${activity} ${name} you can write ${lang} code la
ter!!! `);

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

});

• Lets start by default by asking first question.

ask(0);

• Run node stdInOut


• First question should be displayed. And on answering, next question should be displayed. It should
continue till all questions are answered.
• Post answering all questions, the terminal should display the answers submitted.

varquestions = [

“What is your name?",

"What would you rather be doing?",

"What is your preferred programming language?"

];

varanswers = [];

functionask(i) {

process.stdout.write(`\n\n${questions[i]} >`);

process.stdin.on('data', function(data) {

answers.push(data.toString().trim());

if (answers.length < questions.length) {

ask(answers.length);

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

} else {

process.exit();

});

process.on('exit', function() {

process.stdout.write(`\n\n\n${answers[1]}${answers[0]}, ${answers[2]} is awesome!\n\n`);

});

ask(0);

Output:

node ask

What is your name? > Programmer

What is your fav hobby? > Programming

What is your preferred programming language? > Javascript

Programming Programmer, Javascript is awesome!

Stream interface provides to read and write data. We can use this to communicate with files, the internet,
other processes. In fact, we been using this in the above example and let's go through this with another
example on file streams.

App1:

• Create a readStream.js and import fs


• const fs = require(“fs”);

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

• lets use readStream to read the file chunk by chunk instead of reading the entire file at once.
• const readStream = fs.createReadStream(“./readFile.txt”,”UTF-8”);
• To capture this reading, lets listen to the readStream.on data event.
• readStream.on(“data”, data => {

console.log(`I read ${data}`);

});

• run node readStream.js


• Output should display the content of file chunk by chunk.

The advantage of reading bit by bit is that we need not incur a lot of memory for reading the entire file.
Also, we can control because of raise events.

For eg. readStream.once(“data”, … ); to display the very first little bit of data read. To know that reading is
ended, we can listen to ‘end’ event.

constfs = require("fs");

constreadStream = fs.createReadStream("./readFile.txt", "UTF-8");

console.log("type something...");

readStream.on("data", data=> {

console.log(`I read ${data}`);

});

readStream.once("data", data=> {

console.log("reading once");

});

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

readStream.on("end", () => {

console.log(`reading completed`);

});

Even we can pipe it to another writable stream to update the read stream. To do this, let's understand the
writable stream with an example.

App2:

We have already used a writable stream i.e. process.stdout.write(“hello world”); Let's use the fs module to
write data to file.

• Create a file writeStream.js


• const fs = require(“fs”);
• const writeStream = fs.createWriteStream(“./myFile.txt”,”UTF-8”);
• writeStream.write(“hello world”);
• run node writeStream
• Output has nothing in terminal as the data is written to file. So open the file and see that hello world
exists.
• We can extend this, to read from the terminal and write back to the file system.

process.stdin.on(“data”, data => {

writeStream.write(data);

• run node writeStream


• now the terminal is waiting for us to type something.
• Enter some text and observe that the content is written back to file.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

App3:

Lets further extend this example to incorporate the readStream to read from a file and update the
writeStream.

• const readStream = fs.createReadStream(“./readFile.txt”,”UTF-8”);


• replace the process.stdin with the readStream
• readStream.on(“data”, …);
• run node writeStream
• Observe that the content of the readFile.txt is been read chunk by chunk and written to myFile.txt.

Normally, any readable stream is designed to work with any writable stream. This helps us in piping us the
readable stream with a writable stream.

For example:

• process.stdin.pipe(writeStream);

// this way what ever we type in terminal is been written to the writeStream i.e. the file.

• We can directly use the readStream i.e.


• readStream.pipe(writeStream);

constfs = require("fs");

constwriteStream = fs.createWriteStream("./myFile.txt", "UTF-8");

constreadStream = fs.createReadStream("./readFile.txt", "UTF-8");

readStream.pipe(writeStream);

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

HTTP Module

In this topic, we are going to understand the HTTP module. It is used for making a web server to handle
requests and provide responses. There are two modules mentioned below:

http: https://nodejs.org/api/http.html

https: https://nodejs.org/api/https.html

Both these modules are similar but when we want to work on a secure module then we are going to use
https. And for which we need to supply a secure certificate whereas for http this is not needed.

Let's start using https module to make a request to some page say Wikipedia of Sachin Tendulkar who is
known as the god of cricket. https://en.wikipedia.org/wiki/Sachin_Tendulkar.

In this URL, the domain is en.wikipedia.org and route is wiki/sachin_tendulkar.

App1:

• Let's create a request.js file and import https module.

var https = require(‘https’);

// since Wikipedia is served in https, we are https. If it has to be another site, which serves http, we would
have used http and the rest of the code remains the same.

• Also we need a fs module to stream the response to a file.

var fs = require(‘fs’);

• To make a request we need to build options to it. In our case i.e. wikipedia

var options = { hostname: ‘en.wikipedia.org’, port: 443, path: ‘/wiki/Sachin_Tendulkar’, method: ‘GET’ }

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

• Let’s make a http request with above options with a callback to accept response.

var request = https.request(options, callback);

• Response is a stream object i.e. our Wikipedia page provided in binary format and lets listen to this
stream object i.e. on data event and store it to file system.

var callback = (res) => {

var responseBody = ‘’;

console.log(‘response from server started’);

console.log(`Server status: ${res.statusCode}`);

console.log(`Response headers: ${res.headers}`);

res.setEncoding(‘UTF-8’); // since data is in binary

res.on(‘data’, (chunk) => {

console.log(`--data length--${chunk.lenghth}`);

responseBody = += chunk;

});

res.on(‘end’,() => {

fs.writeFile(‘SachinTendulkar.html’, responseBody, (err) => {

if (err) { throw err;}

console.log(‘File is downloaded’);

});

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

});

• To see if there are any errors, listen to error event on req object

req.on(‘err’, (err) => {

console.log(err.message);

});

• Finally end our request i.e. req.end();


• Run node request.js and observe that new file is created with Sachin-Tendulkar.html

varhttps = require("https");

varfs = require("fs");

varoptions = { hostname:"en.wikipedia.org", port:443, path:"/wiki/Sachin_Tendulkar", method:"GET" };

varreq = https.request(options, function(res) {

varresponseBody = "";

console.log("Response from server started.");

console.log(`Server Status: ${res.statusCode} `);

console.log("Response Headers: %j", res.headers);

res.setEncoding("UTF-8");

res.on("data", function(chunk) {

console.log(`--data length-- ${chunk.length}`);

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

responseBody += chunk;

});

res.on("end", function() {

fs.writeFile("Sachin-Tendulkar.html", responseBody, function(err) {

if (err) { throwerr; }

console.log("File Downloaded");

});

});

});

req.on("error", function(err) {

console.log(err.message);

});

req.end();

App2: Let's build a web server but this time with http module. So we don’t need a secure certificate.

• Create a server.js file and import http

var http = require(‘http’);

• Let’s create a server using http

var server = http.createServer((req, res) => {

step 3

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

});

• Here we are going write headers to the response and end on completion.

res.writeHead(200, {‘Content-Type’: ‘text/plain’});

res.end(‘Hello world’);

• Finally, we need to set a port on which we can listen and log serve has started listening

server.listen(3000);

console.log(‘Server is listening on port: 3000’);

• Now run node server and see that server is listening on port 3000
• Try to navigate to localhost:3000 and see how it responds with the plain text ‘Hello world’.
• We can further extend this by writing an HTML content and changing the header to text/html. Also,
we can find the request URL and request method from the request object. Based on URL and
method we can response accordingly.

Note: If we want to build an https server, we need to provide options to createServer as shown below:

const options = {

key: fs.readFileSync('./agent2-key.pem'),

cert: fs.readFileSync('./agent2-cert.pem')

https.createServer(options,(req, res)=>{…});

or

const options ={

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

pfx: fs.readFileSync('./test_cert.pfx'),

passphrase:'sample'

};

App3: Now build a server to serve files.

• Let's create a fileserver.js and create a folder ‘public’ with few files in it.
• Open fileserver file and import the following modules.
• var http = require(‘http’);
• var fs = require(‘fs’);
• var path = require(‘path’);
• create a http server and listen to 3000 port

http.createServer((req,rest) => {

console.log(`${req.method} request for ${req.url}`);

}).listen(3000);

console.log("File server running on port 3000");

• Now based on the url, we will serve the files inside the public folder i.e default page (may be
previous sachin-tendulkar.html page downloaded) or image (windows provide default pictures
copy one to the public folder say Jellyfish.jpg).

If (req.url === ‘/’) {

fs.readFile("./public/default.html", "UTF-8", function(err, html) {

res.writeHead(200, {"Content-Type": "text/html"});

res.end(html);

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

});

// refer to file system topic to understand this better

} else if (req.url.match(/.jpg$/)) {

var imgPath = path.join(__dirname, 'public', req.url);

var imgStream = fs.createReadStream(imgPath);

res.writeHead(200, {"Content-Type": "image/jpeg"});

imgStream.pipe(res);

// refer to stream topic.

• For any other page, lets respond invalid request

else {

res.writeHead(404, {"Content-Type": "text/plain"});

res.end("404 Not Found");

• Run node fileserver and navigate to localhost:3000 and observe the sachin-tendulkar page is
rendered.
• If we navigate to localhost:3000/jellyfish.jpg then it should return the image. For other page url
then we get 404 Not Found.

varhttp = require("http");

varfs = require("fs");

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

varpath = require("path");

http.createServer(function(req, res) {

console.log(`${req.method} request for ${req.url}`);

if (req.url === "/") {

fs.readFile("./public/default.html", "UTF-8", function(err, html) {

res.writeHead(200, {"Content-Type":"text/html"});

res.end(html);

});

} elseif (req.url.match(/.jpg$/)) {

varimgPath = path.join(__dirname, 'public', req.url);

varimgStream = fs.createReadStream(imgPath);

res.writeHead(200, {"Content-Type":"image/jpeg"});

imgStream.pipe(res);

} else {

res.writeHead(404, {"Content-Type":"text/plain"});

res.end("404 Not Found");

}).listen(3000);

console.log("File server running on port 3000");

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Above example can be extended to serve JSON as content type based on URL as shown below:

res.writeHead(200, {"Content-Type": "text/json"});

res.end(JSON.stringify(data))

Here data could be content of the file with JSON extension.

App4:

All the above examples are serving ‘GET’ method. Lets extend it further to serve a FORM this time. Inside
this form, we will have submit button and on click of which will do a ‘POST’ method to our server.

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Fill out this Form</title>

</head>

<body>

<h 1>Fill out this form</h 1>

<form action="/" method="post">

<label for="first">First name</label>

<input type="text" id="first" name="first" required />

<label for="last">Last name</label>

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

<input type="text" id="last" name="last" required />

<label for="email">Email</label>

<input type="text" id="email" name="email" required />

<button>Submit</button>

</form>

</body>

</html>

• Create a file formserver.js and start a server on port 3000.

var http = require("http");

var fs = require("fs");

http.createServer( (req, res) => {

}).listen(3000);

console.log("Form server listening on port 3000");

• Let’s add conditional code based on request method ‘GET’ and ‘POST’.

if (req.method === "GET") {

} else if (req.method === "POST") {

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

• In GET method case, lets respond with the default.html containing FORM mentioned above.

res.writeHead(200, {"Content-Type": "text/html"});

fs.createReadStream("./default.html", "UTF-8").pipe(res);

• In POST method, listen to data event and end event on request object to capture user data and
respond.

var body = "";

req.on("data", (chunk) => {

body += chunk;

});

req.on("end", function() {

res.writeHead(200, {"Content-Type": "text/html"});

res.end(`

<!DOCTYPE html>

<html>

<head>

<title>Form Results</title>

</head>

<body>

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

<h 1>Your Form Results</h 1>

<p>${body}</p>

</body>

</html>

`);

});

1. Run node formserver and see terminal output ‘Form server listening on port 3000’
2. Navigate to localhost:3000 and see the form is rendered.
3. Fill the details and click on submit button and observe that data is captured.

varhttp = require("http");

varfs = require("fs");

http.createServer(function(req, res) {

if (req.method === "GET") {

res.writeHead(200, {"Content-Type":"text/html"});

fs.createReadStream("./default.html", "UTF-8").pipe(res);

} elseif (req.method === "POST") {

varbody = "";

req.on("data", function(chunk) {

body += chunk;

});

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

req.on("end", function() {

res.writeHead(200, {"Content-Type":"text/html"});

res.end(`

<!DOCTYPE html>

<html>

<head>

<title>Form Results</title>

</head>

<body>

<h 1>Your Form Results</h 1>

<p>${body}</p>

</body>

</html>

`);

});

}).listen(3000);

console.log("Form server listening on port 3000");

On post form submission:

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Your Form Results

first=firstName&last=lastName&[email protected]

UNIT III
MONGO DB
Understanding NoSQL and MongoDB – Building MongoDB Environment – User accounts–Access control – Administering databases
– Managing collections – Connecting to MongoDB from Node.js – simple applications.

Understanding NoSQL and MongoDB


MongoDB, the most popular NoSQL database, is an open-source document-oriented database.

The term ‘NoSQL’ means ‘non-relational’. It means that MongoDB isn’t based on the table-like
relational database structure but provides an altogether different mechanism for storage and
retrieval of data. This format of storage is called BSON ( similar to JSON format).
{
title: 'Geeksforgeeks',
by: 'Harshit Gupta',
url: 'https://www.geeksforgeeks.org',
type: 'NoSQL'
}

Records in a MongoDB database are called documents, and the field values may include numbers,
strings, booleans, arrays, or even nested documents.
{
title: "Post Title 1",
body: "Body of post.",
category: "News",
likes: 1,
tags: ["news", "events"],
date: Date()
}

Features of MongoDB:
• Document Oriented: MongoDB stores the main subject in the minimal number of
documents and not by breaking it up into multiple relational structures like RDBMS. For

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

example, it stores all the information of a computer in a single document called Computer and
not in distinct relational structures like CPU, RAM, Hard disk, etc.
• Indexing: Without indexing, a database would have to scan every document of a collection to
select those that match the query which would be inefficient. So, for efficient searching
Indexing is a must and MongoDB uses it to process huge volumes of data in very less time.
• Scalability: MongoDB scales horizontally using sharding (partitioning data across various
servers). Data is partitioned into data chunks using the shard key, and these data chunks are
evenly distributed across shards that reside across many physical servers. Also, new machines
can be added to a running database.
• Replication and High Availability: MongoDB increases the data availability with multiple
copies of data on different servers. By providing redundancy, it protects the database from
hardware failures. If one server goes down, the data can be retrieved easily from other active
servers which also had the data stored on them.
• Aggregation: Aggregation operations process data records and return the computed results.
It is similar to the GROUPBY clause in SQL. A few aggregation expressions are sum, avg, min,
max, etc.

NoSQL

➢ NoSQL database is a database that stores data in a format other than relational tables.
➢ NoSQL databases are flexible, scalable, and distributed databases.
➢ NoSQL, as opposed to SQL, is a database management approach, whereas SQL is just a query
language, similar to the query languages of NoSQL databases.
➢ NoSQL databases (AKA "not only SQL") store data differently than relational tables.
➢ NoSQL databases come in a variety of types based on their data model. The main types
are document, key-value, wide-column, and graph. They provide flexible schemas and scale
easily with large amounts of big data and high user loads.

Types of NoSQL databases :

1. document databases,
2. key-value databases,
3. wide-column stores,
4. graph databases.

1.Document-oriented databases

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

A document-oriented database stores data in documents similar to JSON (JavaScript Object


Notation) objects. Each document contains pairs of fields and values. The values can typically be a
variety of types, including things like strings, numbers, booleans, arrays, or even other objects. A
document database offers a flexible data model, much suited for semi-structured and typically
unstructured data sets. They also support nested structures, making it easy to represent complex
relationships or hierarchical data.

Examples of document databases are MongoDB and Couchbase. A typical document will look like
the following:

{
"_id": "12345",
"name": "foo bar",
"email": "[email protected]",
"address": {
"street": "123 foo street",
"city": "some city",
"state": "some state",
"zip": "123456"
},
"hobbies": ["music", "guitar", "reading"]
}
2.Key-value databases
A key-value store is a simpler type of database where each item contains keys and values.
Each key is unique and associated with a single value. They are used for caching and session
management and provide high performance in reads and writes because they tend to store things
in memory. Examples are Amazon DynamoDB and Redis. A simple view of data stored in a key-value
database is given below:
Key: user:12345
Value: {"name": "foo bar", "email": "[email protected]", "designation": "software developer"}
3.Wide-column stores
Wide-column stores store data in tables, rows, and dynamic columns. The data is stored in
tables. However, unlike traditional SQL databases, wide-column stores are flexible, where different
rows can have different sets of columns. These databases can employ column compression
techniques to reduce the storage space and enhance performance. The wide rows and columns
enable efficient retrieval of sparse and wide data. Some examples of wide-column stores are Apache
Cassandra and HBase. A typical example of how data is stored in a wide-column is as follows:

name id email dob city

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Foo bar 12345 [email protected] Some city

Carn Yale 34521 [email protected] 12-05-1972

4.Graph databases

A graph database stores data in the form of nodes and edges. Nodes typically store
information about people, places, and things (like nouns), while edges store information about the
relationships between the nodes. They work well for highly connected data, where the relationships
or patterns may not be very obvious initially. Examples of graph databases are Neo4J and Amazon
Neptune. MongoDB also provides graph traversal capabilities using the $graphLookup stage of the
aggregation pipeline.

Install MongoDB On Windows

To install MongoDB on Windows, first download the latest release of MongoDB


from https://www.mongodb.com/download-center.

Enter the required details, select the Server tab, in it you can choose the version of MongoDB,
operating system and, packaging as:

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Now install the downloaded file, by default, it will be installed in the folder C:\Program Files\.

MongoDB requires a data folder to store its files. The default location for the MongoDB data
directory is c:\data\db. So you need to create this folder using the Command Prompt. Execute the
following command sequence.

C:\>md data
C:\md data\db

Then you need to specify set the dbpath to the created directory in mongod.exe. For the same,
issue the following commands.

In the command prompt, navigate to the bin directory current in the MongoDB installation folder.
Suppose my installation folder is C:\Program Files\MongoDB

C:\Users\XYZ>d:cd C:\Program Files\MongoDB\Server\4.2\bin

C:\Program Files\MongoDB\Server\4.2\bin>mongod.exe --dbpath "C:\data"

This will show waiting for connections message on the console output, which indicates that the
mongod.exe process is running successfully.

Now to run the MongoDB, you need to open another command prompt and issue the following
command.

C:\Program Files\MongoDB\Server\4.2\bin>mongo.exe
MongoDB shell version v4.2.1
connecting to:
mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Implicit session: session { "id" : UUID("4260beda-f662-4cbe-9bc7-5c1f2242663c") }


MongoDB server version: 4.2.1
>

This will show that MongoDB is installed and run successfully. Next time when you run MongoDB,
you need to issue only commands.

C:\Program Files\MongoDB\Server\4.2\bin>mongod.exe --dbpath "C:\data"

C:\Program Files\MongoDB\Server\4.2\bin>mongo.exe

Now issue the following command to update the repository −

sudo apt-get update

Next install the MongoDB by using the following command −

apt-get install mongodb-10gen = 4.2

In the above installation, 2.2.3 is currently released MongoDB version. Make sure to install the latest
version always. Now MongoDB is installed successfully.

Start MongoDB
sudo service mongodb start

Stop MongoDB
sudo service mongodb stop

Restart MongoDB

sudo service mongodb restart

To use MongoDB run the following command.

mongo

This will connect you to running MongoDB instance.

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

MongoDB is an open-source document-oriented database that is designed to store a large scale of


data and also allows you to work with that data very efficiently.

Advantages of MongoDB
• It is a schema-less NoSQL database. You need not to design the schema of the database when
you are working with MongoDB.
• It does not support join operation.
• It provides great flexibility to the fields in the documents.
• It contains heterogeneous data.
• It provides high performance, availability, scalability.
• It supports Geospatial efficiently.
• It is a document oriented database and the data is stored in BSON documents.
• It also supports multiple document ACID transition(string from MongoDB 4.0).
• It does not require any SQL injection.
• It is easily integrated with Big Data Hadoop
Disadvantages of MongoDB
• It uses high memory for data storage.
• You are not allowed to store more than 16MB data in the documents.
• The nesting of data in BSON is also limited you are not allowed to nest data more than 100
levels.

Creating a Database
With access control enabled, users are required to identify themselves. You have to grant a user one or
more roles. A role grants a user privileges to perform certain actions on MongoDB resources.
db.createUser(
{
user: "myTester",
pwd: passwordPrompt(), // or cleartext password
roles: [ { role: "readWrite", db: "test" },
{ role: "read", db: "reporting" } ]
}
)

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

Insert a document
As the user myTester, you have privileges to perform read and write operations in the test database (as well
as perform read operations in the reporting database). Once authenticated as myTester, insert a document
into a collection in the test database. For example, you can perform the following insert operation in the test
database:
db.foo.insertOne( { x: 1, y: 1 } )

Username/Password Authentication

use reporting
db.createUser(
{
user: "reportsUser",
pwd: passwordPrompt(), // or cleartext password
roles: [
{ role: "read", db: "reporting" },
{ role: "read", db: "products" },
{ role: "read", db: "sales" },
{ role: "readWrite", db: "accounts" }
]
}
)

Connect to MongoDB from Node.js

Create a file to contain your application called index.js in your node_quickstart project directory.
Copy and paste the following code into the index.js file:

const { MongoClient } = require("mongodb");


// Replace the uri string with your connection string.
const uri = "<connection string uri>";
const client = new MongoClient(uri);
async function run() {
try {
const database = client.db('sample_mflix');
const movies = database.collection('movies');
// Query for a movie that has the title 'Back to the Future'
const query = { title: 'Back to the Future' };
const movie = await movies.findOne(query);
console.log(movie);
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}

Downloaded by gowsalyaa m ([email protected])


lOMoARcPSD|34806321

St. PETER'S
COLLEGE OF ENGINEERING & TECHNOLOGY
Affiliated to Anna University | Approved by AICTE
Avadi, Chennai, Tamilnadu – 600 054
Phone: 7358110159/56 Website: www.spcet.ac.in Email: [email protected]

}
run().catch(console.dir);

2.Assign the Connection String:

Replace the <connection string uri> placeholder with the connection string that you copied from the Create
a Connection String step of this guide.

3.Run your Node.js Application:

In your shell, run the following command to start this application:

node index.js

The output includes details of the retrieved movie document:

{
_id: ...,
plot: 'A young man is accidentally sent 30 years into the past...',
genres: [ 'Adventure', 'Comedy', 'Sci-Fi' ],
...
title: 'Back to the Future',
...
}

Downloaded by gowsalyaa m ([email protected])

You might also like