Lecture Notes
Lecture Notes
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]
Year/Sem: III/V
LECTURE NOTES
(Regulation 2021)
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]
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.
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
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.
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]
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]
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]
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 model is responsible for the business logic and data of the application.
• The controller establishes and maintains communication between the model and the view and manages the
application logic.
Components of MVC
• Controller
• Model
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
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:
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.
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:
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:
Example: In a bookstore application, the Model would handle data related to books, such as the book title,
author, price, and stock level.
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.
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 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
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
• It reduces complexity by dividing an application into three units. Model, view, and controller.
• 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
Disadvantages of MVC
• The framework navigation can be complex as it introduces new layers of abstraction which requires
users to adapt to the decomposition criteria of MVC.
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.
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.
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.
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.
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
o The user interacts with the View, such as clicking a button or entering text into a form.
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.
o It interprets the input, performs any necessary operations (such as updating the Model), and
decides how to respond.
o The Controller updates the Model based on the user input or application logic.
o The View requests data from the Model to update its display.
o The Controller updates the View based on the changes in the Model or in response to user
input.
o The View renders the updated UI based on the changes made by the Controller.
Below is the code of above problem statement using MVC Design Pattern:
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]
Represents the data (student’s name and roll number) and provides methods to access and modify this data.
• Java
class Student {
return rollNo;
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.rollNo = rollNo;
return name;
this.name = name;
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 {
System.out.println("Student:");
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]
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 {
this.model = model;
this.view = view;
model.setName(name);
return model.getName();
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]
model.setRollNo(rollNo);
return model.getRollNo();
view.printStudentDetails(model.getName(), model.getRollNo());
• Java
class Student {
return rollNo;
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.rollNo = rollNo;
return name;
this.name = name;
class StudentView {
System.out.println("Student:");
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 {
this.model = model;
this.view = view;
model.setName(name);
return model.getName();
model.setRollNo(rollNo);
return model.getRollNo();
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.printStudentDetails(model.getName(), model.getRollNo());
controller.updateView();
controller.setStudentName("Vikram Sharma");
controller.updateView();
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.setName("Lokesh Sharma");
student.setRollNo("15UCS157");
return student;
• Output
Student:
Student:
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.
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.
• 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.
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.
• 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.
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.
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.
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. 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.
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.
• 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.
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.
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]
category: "News",
likes: 1,
date: Date()
MongoDB use:
• Auto-sharding
• Rich queries
• Data Hub
React
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.
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
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 file
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.
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.
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]
➢ 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.
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:
➢ 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:
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\.
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.
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]
Seventh, you need to wait for the setup wizard to install Node.js:
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)
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 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:
In this example we download the module express. This works with the following command:
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.
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.
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]
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.
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
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.
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 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
Hello World
NPM (Node Package Manager) is the default package manager for Node and is written entirely
in JavaScript.
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.
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:
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('saved', () => {
console.log(`A saved event occurred.`);
});
Output:
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:
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.
To detach an event listener from an event, you use the off() method of the EventEmitter object. For
example:
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]
// no effect
emitter.emit('saved', {
id: 2,
name: 'Jane Doe'
});Code language: JavaScript (javascript)
Output:
In this example, after detaching the event listener from the event. The event listener is not called when
the saved event is emitted.
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.
stock.price = 720;
Code language: JavaScript (javascript)
Output:
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.
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.
• 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.
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 currentTime = 0;
• 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 currentTime = 0;
setInterval(() => {
currentTime =+ waitInterval;
}, waitInterval);
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 currentTime = 0;
currentTime =+ waitInterval;
}, 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:
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.
function writeWaitingPercent(percent) {
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.
currentTime =+ waitInterval;
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]
writeWaitingPercent(percentWaited);
}, waitInterval);
varwaitTime = 3000;
varcurrentTime = 0;
varwaitInterval = 500;
varpercentWaited = 0;
functionwriteWaitingPercent(percent) {
process.stdout.clearLine();
process.stdout.cursorTo(0);
varinterval = setInterval(function() {
currentTime += waitInterval;
writeWaitingPercent(percentWaited);
}, waitInterval);
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);
}, waitTime);
writeWaitingPercent(percentWaited);
Output:
node timers
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.
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]
App2:
One other important about these events is that they are asynchronous and raised when they happen. Let
take another example.
});
1. If user enters ‘exit’, then lets exit the process by emiting that user has exited.
2. process.stdin.on(“data”, data => {
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();
});
• 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.
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.
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]
console.log(`${this.name}`: ${msg});
});
varEventEmitter = require('events').EventEmitter;
varutil = require('util');
varPerson = function(name) {
this.name = name;
};
util.inherits(Person, EventEmitter);
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}`);
});
Output:
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:
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.
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:
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:
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
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]
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;
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.
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.
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:
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.
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]
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 = [
];
• Create an arrow function which takes an index and writes the question out
process.stdout.write(`\n\n\n ${questions[i]}`);
};
ask();
• 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.
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]
answers.push(data.toString().trim());
});
• Lets keep asking one question after every question till all are answered. And once done, we exit.
answers.push(data.toString().trim());
ask(answers.length);
} else {
process.exit();
});
• Lets add one more event handler on exit to acknowledge that all answers are captured.
process.on("exit", () => {
process.stdout.write.log(`Thank you for your anwsers. Go ${activity} ${name} you can write ${lang} code la
ter!!! `);
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]
});
ask(0);
varquestions = [
];
varanswers = [];
functionask(i) {
process.stdout.write(`\n\n${questions[i]} >`);
process.stdin.on('data', function(data) {
answers.push(data.toString().trim());
ask(answers.length);
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() {
});
ask(0);
Output:
node ask
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:
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 => {
});
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");
console.log("type something...");
readStream.on("data", data=> {
});
readStream.once("data", data=> {
console.log("reading once");
});
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.
writeStream.write(data);
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.
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.
constfs = require("fs");
readStream.pipe(writeStream);
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.
App1:
// 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.
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’ }
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.
• 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.
console.log(`--data length--${chunk.lenghth}`);
responseBody = += chunk;
});
res.on(‘end’,() => {
console.log(‘File is downloaded’);
});
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
console.log(err.message);
});
varhttps = require("https");
varfs = require("fs");
varresponseBody = "";
res.setEncoding("UTF-8");
res.on("data", function(chunk) {
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() {
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.
step 3
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.end(‘Hello world’);
• Finally, we need to set a port on which we can listen and log serve has started listening
server.listen(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 ={
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'
};
• 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) => {
}).listen(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).
res.end(html);
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 if (req.url.match(/.jpg$/)) {
imgStream.pipe(res);
else {
• 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");
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) {
res.writeHead(200, {"Content-Type":"text/html"});
res.end(html);
});
} elseif (req.url.match(/.jpg$/)) {
varimgStream = fs.createReadStream(imgPath);
res.writeHead(200, {"Content-Type":"image/jpeg"});
imgStream.pipe(res);
} else {
res.writeHead(404, {"Content-Type":"text/plain"});
}).listen(3000);
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.end(JSON.stringify(data))
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">
</head>
<body>
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]
<label for="email">Email</label>
<button>Submit</button>
</form>
</body>
</html>
var fs = require("fs");
}).listen(3000);
• Let’s add conditional code based on request method ‘GET’ and ‘POST’.
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.
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.
body += chunk;
});
req.on("end", function() {
res.end(`
<!DOCTYPE html>
<html>
<head>
<title>Form Results</title>
</head>
<body>
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]
<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) {
res.writeHead(200, {"Content-Type":"text/html"});
fs.createReadStream("./default.html", "UTF-8").pipe(res);
varbody = "";
req.on("data", function(chunk) {
body += chunk;
});
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>
<p>${body}</p>
</body>
</html>
`);
});
}).listen(3000);
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=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.
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
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.
1. document databases,
2. key-value databases,
3. wide-column stores,
4. graph databases.
1.Document-oriented databases
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]
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:
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]
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.
Enter the required details, select the Server tab, in it you can choose the version of MongoDB,
operating system and, packaging as:
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
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
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 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>mongo.exe
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
mongo
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]
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" } ]
}
)
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" }
]
}
)
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:
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);
Replace the <connection string uri> placeholder with the connection string that you copied from the Create
a Connection String step of this guide.
node index.js
{
_id: ...,
plot: 'A young man is accidentally sent 30 years into the past...',
genres: [ 'Adventure', 'Comedy', 'Sci-Fi' ],
...
title: 'Back to the Future',
...
}