You are on page 1of 21

A

Industrial Training
ReportOn
“Node JS”
Submitted for partial fulfilment of the requirements for the award of
the degree of
Bachelor of
Technology In
Electrical Engineering

Session : 2023-24

Submitted to:- Submitted by:-


Dept. of Electrical Engineering Lalit soni
20ESKEE061
VII Sem
Electrical Engineering

Department of Electrical Engineering


Swami Keshvanand Institute of Technology, Management & Gramothan
Ramnagaria, Jagatpura, Jaipur (RAJ.)
CERTIFICATE

Department of Electrical Engineering, SKIT M&G, Page


Jaipur
Swami Keshvanand Institute of Technology,
Management and Gramothan, Jaipur

CANDIDATE DECLARATION

I, Lalit Soni, a student of VII semester, B.Tech. (Electrical Engineering)


has completed my industrial training from "Celebal Technologies",
hereby declare that the industrial training report submitted by me for
partial fulfilment of the requirements for the award of the degree of
B.Tech from Rajasthan Technical University, Kota, during the session
2022-23, is a result of my own work. I solemnly affirm that any data or
content used in this report is either original or appropriately cited.
Furthermore, if any part of the report is found to be copied, it has been
typed by me, and due credit is given to the original source. I understand
the consequences of plagiarism and take full responsibility for the
integrity of this document.

Lalit soni
7 EE B
20ESKEE061

Date: 04/12/2023 Mr. Ajay Bhardwaj


Place: Jaipur Incharge, Industrial
Training Dept. of EE, SKIT,
Jaipur

Department of Electrical Engineering, SKIT M&G, Page


Jaipur
ACKNOWLEDGEMENT

I would like to show gratitude towards my teachers and the trainers for the
opportunity given and help to complete my training within the time allotted.

This training would not have been completed without the support of my teachers and
parents. Therefore, I would like to thank our Incharge, Industrial Training, Mr. Ajay
Bhardwaj, Assistant Professor, Department of Electrical Engineering, Swami
Keshvanand Institute of Technology, Management & Gramothan, Jaipur for
extending his cordial support throughout the training completion. I would also like to
thank Dr. Sarfaraz Nawaz, Head Of Department of Electrical Engineering, Swami
Keshvanand Institute of Technology, Management & Gramothan, Jaipur for
giving me enough time to complete the training.

I would like to extend my thanks to Dr. S. L. Surana, Director (Academics) and Dr.
Ramesh Kumar Pachar, Principal, Swami Keshvanand Institute of Technology,
Management & Gramothan, Jaipur for their motivation throughout our college time.

Our efforts made together led to the effective completion of the training.

It may not be possible for me to acknowledge the support of all my friends, but I am
thankful to all other trainees for their valuable ideas and support during the training
period.

LALIT SONI
7 EE B
20ESKEE061

Department of Electrical Engineering, SKIT M&G, Page


Jaipur
CONTENTS

Main Page
Certificate
Candidate Declaration
Acknowledgement
Contents
Abstract

Chapter name
1. History

2. Introduction
2.1 NodeJS
2.2 Purpose and goals of NodeJS

3. Architecture of NodeJS
3.1 Overview of the event-Driven, Non-Blocking I/O mode
3.2 NodeJS process Architecture and Asynchronous programming
in NodeJS

4. Features of NodeJS
4.1 Source-side JavaScript

5. Express.JS Framework
5.1 Introduction

6. Database Connectivity
6.1 connecting database using NodeJS

7. Scalability and Performance


7.1 Techniques for improving NodeJS performance

Conclusion
Reference

Department of Electrical Engineering, SKIT M&G, Page


Jaipur
ABSTRACT

This report encapsulates a transformative Node.js internship experience focusing on


core concepts, Express.js, database connectivity, scalability, and performance
optimization. The internship aimed to equip participants with advanced web
development skills, emphasizing real-world application. Highlights include a deep
dive into Node.js fundamentals, mastering Express.js, practical database connectivity,
scalability techniques, and the implementation of a real-world project. The report
showcases acquired proficiency, emphasizing the participant's journey into the
dynamic world of Node.js.

Department of Electrical Engineering, SKIT M&G, Page


Jaipur
1. HISTORY

JavaScript is a programming language that was created at Netscape as a scripting tool to


manipulate web pages inside their browser, Netscape Navigator. Part of the business model
of Netscape was to sell Web Servers, which included an environment called Netscape
LiveWire that could create dynamic pages using server-side JavaScript.

Unfortunately,
Netscape LiveWire wasn't very successful and server-side Javascript wasn't popularized
until recently, by the introduction of Node.js. One key factor that led to the rise of Node.js
was the timing. Just a few years earlier, JavaScript had started to be considered as a more
serious language, thanks to "Web 2.0"applications (such as Flickr, Gmail, etc.) that showed
the world what a modern experience on thewebcould be like.

JavaScript engines also became considerably better as many browsers competed to offer
users the best performance. Development teams behind major browsers worked hard to
offer better support for JavaScript and find ways to make JavaScript run faster. The engine
hat Node.jsusesunderthehood, V8 (alsoknown as Chrome V8 forbeingtheopen-source JavaScript
engine of The Chromium Project), improved significantly due to this competition.

Node.js happened to be built in the right place and right time, but luck isn't the only reason
why it is popular today. It introduces a lot of innovative thinking and approaches for
JavaScript server-side development that has already helped so many developers

Department of Electrical Engineering, SKIT M&G, Page


Jaipur
2. INTRODUCTION

2.1 NODE.JS

Node.js is a server-side platform built on Google Chrome's JavaScript Engine


(V8 Engine). Node.js was developed by Ryan Dahl in 2009 and its latest version is v0.10.36.
Thedefinition of Node.js as supplied by its official documentation
is as follows –

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast and
scalable network applications. Node.js uses an event-driven, non-blocking I/O model that
makes it lightweight and efficient, perfect for data-intensive real-time applications that run
across distributed devices. Node.js is an open source, cross-

platform runtime environment for developing server-side and networking applications.


Node.js applications are written in JavaScript, and can be run within the Node.js runtime on
OS X, Microsoft Windows, and

Linux. Node.js also provides a rich library of various JavaScript modules which simplifies th
e development of web applications using Node.js to a great extent.

Department of Electrical Engineering, SKIT M&G, Page


Jaipur
2.2 Purpose and goals of NodeJS

Node.js was designed with several key objectives in mind:

 High Performance: Leveraging the asynchronous, event-driven architecture, Node.js


enables non-blocking I/O operations. This means that it can efficiently handle
multiple requests concurrently, making it well-suited for building fast and scalable
network applications.

 JavaScript Everywhere: Node.js unifies web application development by allowing


developers to use JavaScript for both server-side and client side scripting. This not
only streamlines the development process but also facilitates code reuse and
consistency.

 Scalability: Node.js is particularly well-suited for building applications that require


high scalability, such as real-time applications, chat applications, and collaborative
tools. Its event-driven model allows for the handling of a large number of concurrent
connections without the need for additional threads.

 Large Ecosystem: Node.js comes with a vibrant and extensive ecosystem of packages
and libraries, facilitated by the Node Package Manager (NPM). This makes it easy for
developers to find and use pre-built modules to accelerate development.

 Community Support: Node.js has garnered strong community support over the years.
This active community contributes to the continuous improvement of Node.js, provides
assistance through forums and discussions, and creates a collaborative environment for
developers.

Department of Electrical Engineering, SKIT M&G, Page


Jaipur
3. Architecture of NodeJS

3.1 Overview of the Event-Driven, Non-Blocking I/O Model

Node.js adopts an event-driven, non-blocking I/O model that sets it apart from
traditional server-side technologies. The fundamental concept revolves around
handling operations asynchronously, allowing the server to continue processing other
tasks while waiting for non-blocking I/O operations to complete.

 Event-Driven: Node.js operates based on events and callbacks. Events are


triggered when certain conditions are met, and corresponding callbacks handle
these events. This model is particularly powerful for handling concurrent
operations and managing numerous connections simultaneously.
 Non-Blocking I/O: Traditional servers typically follow a synchronous,
blocking I/O model, where each I/O operation blocks the execution until it
completes. In contrast, Node.js leverages non-blocking I/O, enabling the
server to initiate multiple operations simultaneously without waiting for each
one to finish. This approach significantly enhances the server's efficiency and
responsiveness.
 Event Loop: At the core of Node.js architecture is the event loop. The event
loop continuously checks the message queue for new events or tasks, executes
them, and then moves on to the next iteration. This loop ensures that the server
remains responsive and can handle a large number of concurrent connections
without creating additional threads.

Department of Electrical Engineering, SKIT M&G, Page


Jaipur
3.2 Node.js Process Architecture and Asynchronous programming in
NodeJS

Node.js Process Architecture Node.js employs a single-threaded event loop model to


handle concurrent connections efficiently. However, this doesn't mean Node.js is
limited to running on a single CPU core. It achieves scalability through a combination
of techniques:

 Concurrency Model: While Node.js uses a single-threaded event loop, it can


handle concurrency through the efficient delegation of tasks to the operating
system's kernel and the use of asynchronous operations.

 Libuv Library: Node.js relies on the Libuv library to handle asynchronous I/O
operations. Libuv provides a unified API for asynchronous operations on
various operating systems, including Windows, Linux, and macOS.

 Worker Processes and Clustering: Node.js allows the creation of child


processes to take advantage of multi-core systems. The "cluster" module
facilitates the creation of worker processes that can share the incoming traffic
load, providing a scalable solution for multi-core architectures.

Asynchronous Programming in Node.js

Asynchronous programming is a key aspect of Node.js, enabling developers to write


non-blocking, efficient code.

The primary mechanisms for achieving asynchrony include:

 Callbacks: Callback functions are a fundamental part of Node.js. They are


functions passed as arguments to other functions and are executed once the
task is completed. This mechanism allows developers to initiate operations
and define how the application should respond when those operations finish.

 Promises: Promises provide an alternative to callbacks, offering a cleaner and


more structured way to handle asynchronous operations. Promises represent a
value that might be available now, or in the future, or never.

 Async/Await: Introduced in later versions of Node.js, async/await is a


syntactic sugar over promises, making asynchronous code appear more like
synchronous code. This makes it easier for developers to write and read
asynchronous code, improving code readability

Department of Electrical Engineering, SKIT M&G, Page


Jaipur
4. Features of NodeJS

4.1 Server-side JavaScript

Traditionally, JavaScript was primarily associated with client-side scripting for web
browsers. Node.js extends the use of JavaScript to the server side, allowing developers to use
a single programming language, JavaScript, for both client and server applications. This
unification simplifies development, promotes code reuse, and facilitates the sharing of code
between the server and client. It also enables developers with expertise in JavaScript to work
on full-stack development seamlessly.

NPM (Node Package Manager)

Node Package Manager (NPM) is a powerful package manager and ecosystem for Node.js. It
simplifies the process of discovering, installing, and managing third-party libraries and tools.
Key aspects of NPM include:

 Package Installation: NPM allows developers to install packages (libraries or tools)


from a vast registry of open-source packages. These packages can be easily integrated
into Node.js projects to add functionality or streamline development processes.

 Dependency Management: NPM manages project dependencies efficiently. Each


Node.js project can have a package.json file that lists the project's dependencies along
with their versions. This ensures consistency across different environments and makes
it easy to share projects with others.

 Scripts: NPM enables the execution of scripts defined in the package.json file,
automating common development tasks. This is particularly useful for tasks like
testing, building, and deployment.

 Versioning: NPM uses semantic versioning (SemVer), allowing developers to specify


version ranges for dependencies, ensuring that updates are compatible with the
project.

Cross-platform Development

Node.js supports cross-platform development, meaning that applications written in Node.js


can run on various operating systems without modification. This is possible because Node.js
is built on the V8 JavaScript runtime, which itself is designed to be cross-platform.
Developers can write code on one operating system (e.g., Windows) and deploy and run it on
another (e.g., Linux or macOS) seamlessly.

This cross-platform capability is valuable for teams working in diverse environments and for
deploying applications on servers with different operating systems. It fosters flexibility and
reduces the challenges associated with platform-specific development.

Department of Electrical Engineering, SKIT M&G, Page


Jaipur
Single-threaded Event Loop

Node.js follows a single-threaded event loop model for handling concurrency. The event loop
efficiently manages multiple connections without creating additional threads. Key aspects of
the single-threaded event loop include:

 Non-Blocking I/O: The single-threaded event loop enables Node.js to handle a large
number of concurrent connections without waiting for each I/O operation to complete.
Asynchronous operations allow the server to initiate tasks and continue processing
other events while waiting for I/O to finish.

 Scalability: While Node.js is single-threaded, it can handle scalability through the


efficient delegation of tasks to the operating system's kernel and the use of
asynchronous operations. It can also take advantage of multi-core systems by creating
child processes and using the clustering module.

 Responsive Applications: The event loop ensures that Node.js remains responsive,
even under heavy loads. It processes events sequentially, preventing any single long-
running task from blocking the entire application.

Department of Electrical Engineering, SKIT M&G, Page


Jaipur
5. Express.js Framework

5.1 Introduction to Express.js

Express.js Overview:

 Minimally Opininated: Express.js is a lightweight and unopinionated web application


framework for Node.js. It offers essential features without imposing a rigid structure,
giving developers flexibility.

 Routing Simplicity: Express simplifies URL routing, making it easy to define routes
for different HTTP methods and URL patterns. This enhances code organization and
readability.

 Middleware for Functionality: Middleware functions in Express handle tasks like


logging, authentication, and error handling. They add modularity and customization to
the request-response cycle.

Building Web Applications with

Express Development Process:

 Setting Up: Install Express using NPM and set up the basic application structure,
including server file, middleware configuration, and route definitions.

 Routing: Define routes for various HTTP methods and URL patterns. Implement
route handlers to process requests and generate responses.

 Middleware Usage: Leverage middleware functions for tasks like logging,


authentication, and data validation. Middleware executes before or after route
handling, enhancing flexibility.

 Database Integration: Easily integrate Express with databases like MongoDB or


MySQL, allowing seamless interaction with data within the application.

 Templating Engines: Support for various templating engines enables server-side


rendering. Choose an engine (EJS, Handlebars, Pug) based on project requirements.

 Error Handling: Express provides mechanisms for error handling, including custom
error handlers and middleware functions dedicated to handling errors gracefully.

Middleware in

Express.js Middleware

Functions:

 Definition: Middleware functions have access to the request (`req`) and response
(`res`) objects and can manipulate them. They also have access to the next
middleware function in the cycle.

Department of Electrical Engineering, SKIT M&G, Page


Jaipur
 Common Uses:
Logging:
Capture information about incoming requests for debugging and analysis.

Authentication:
Verify user credentials before granting access to specific routes or resources.

Error Handling:
Catch and handle errors during the request-response cycle to prevent application
crashes.

Data Parsing:
Middleware can parse incoming data (JSON, URL-encoded) for easier handling.

CODE:

const express = require('express');


const app = express();

// Middleware to log the request method and URL


app.use((req, res, next) => {
console.log(`[${new Date().toLocaleString()}] ${req.method}
${req.url}`);
next();
});

// Route handler
app.get('/', (req, res) =>
{
res.send('Hello, Express!');
});

app.listen(3000, () => {
console.log('Server is listening on port 3000');
});

Department of Electrical Engineering, SKIT M&G, Page


Jaipur
6. Database Connectivity

6.1 Connecting to Databases Using Node.js

Database Connectivity in Node.js:

 Node.js provides various modules and drivers for connecting to databases. The most
common approach is to use a dedicated database driver or an Object-Relational
Mapping (ORM) library.

 Popular database drivers include mysql for MySQL, mongodb for MongoDB, pg for
PostgreSQL, and others. These drivers enable Node.js applications to establish
connections, execute queries, and interact with the respective databases.

Popular Database

Options 1.MongoDB:

A NoSQL database that stores data in JSON-like BSON format. Known for its flexibility,
scalability, and ability to handle unstructured data. Widely used for document-oriented
databases in applications with varying data structures.

2. MySQL:

A relational database management system (RDBMS) that uses structured


query language (SQL). Known for its reliability, ACID compliance, and compatibility
with various applications. Suitable for applications that require a structured and relational
data model.

3. PostgreSQL:

An open-source, object-relational database system. Known for its advanced features,


extensibility, and support for complex queries. Suitable for applications that require
advanced data modeling and relational integrity.

4. SQLite:

A self-contained, serverless, and zero-configuration SQL database engine. Known for its
simplicity, lightweight nature, and ease of integration. Suitable for small to medium-sized
applications or embedded systems.

Department of Electrical Engineering, SKIT M&G, Page


Jaipur
Using an ORM (Object-Relational Mapping) in Node.js

ORM in Node.js:

 An ORM simplifies database interactions by mapping objects in code to database


tables.

 Eliminates the need for raw SQL queries, making database operations more intuitive
and portable across different database systems.

Benefits of Using an ORM:

Abstraction of Database Details:

 Developers can work with objects and classes in their code, abstracting away the
specifics of the underlying database.

 Portability:
 The same code can be used with different databases by changing the ORM
configuration.

 Productivity: Developers can focus more on application logic, and the ORM takes
care of generating and executing SQL queries.

Popular Node.js ORMs:

 Sequelize: Supports multiple relational databases like MySQL, PostgreSQL, SQLite.

 Mongoose: Designed for MongoDB, providing a schema-based solution and


supporting features like validation.

 TypeORM: Supports various databases, including MySQL, PostgreSQL, SQLite, and


MongoDB. It is written in TypeScript.

Department of Electrical Engineering, SKIT M&G, Page


Jaipur
7. Scalability and Performance

7.1 Techniques for Improving Node.js Performance

1. Asynchronous Code: Leverage Node.js's non-blocking I/O model to ensure that the
application remains responsive even when handling multiple concurrent operations.

2. Code Optimization: Optimize your code for performance by identifying and refactoring
bottlenecks. Tools like profilers and performance monitoring libraries can help in this
process.

3. Caching: Implement caching mechanisms, both at the application and database levels, to
reduce redundant computations and queries.

4. Compression: Use compression middleware to reduce the size of data sent between the
server and clients, improving overall performance, especially for web applications.

Load Balancing and Clustering

1. Load Balancing: Distribute incoming network traffic across multiple servers to ensure no
single server is overwhelmed. Load balancers enhance fault tolerance and enable horizontal
scaling.

2. Horizontal Scaling: Add more machines to your infrastructure to handle increased load.
This is achieved through load balancing, where incoming requests are distributed among
multiple servers.

3. Clustering: Node.js supports clustering, allowing multiple instances of an application to


run on separate processes or even different machines. The cluster module facilitates
communication between these instances.

4. Reverse Proxy: Use a reverse proxy, such as Nginx or HAProxy, to distribute incoming
requests among multiple Node.js instances. This helps in load balancing and provides
additional security features.

Best Practices for Scalability

1. Microservices Architecture: Adopt a microservices architecture to break down your


application into smaller, independent services. Each service can be developed, deployed, and
scaled independently.

2. Database Scaling: Choose databases that support horizontal scaling. Shard your database or
use distributed databases to handle increasing amounts of data.

3. Monitoring and Analytics: Implement robust monitoring and analytics tools to gain
insights into your application's performance. Identify areas for improvement and make data-
driven decisions.

Department of Electrical Engineering, SKIT M&G, Page


Jaipur
4. Cloud Services: Utilize cloud services to easily scale resources based on demand. Cloud
platforms like AWS, Azure, and Google Cloud provide auto-scaling features.

5. Performance Testing: Regularly perform performance testing to identify bottlenecks and


areas for improvement. Tools like Apache JMeter or Artillery can help simulate real-world
scenarios.

6. Stateless Design: Design your application to be as stateless as possible. Store session data
in external systems like Redis to enable easier scaling.

7. CDN (Content Delivery Network): Use a CDN to distribute static assets closer to end-
users, reducing the load on your servers and improving response times.

Department of Electrical Engineering, SKIT M&G, Page


Jaipur
Conclusion

In summary, our exploration of Node.js has uncovered several key aspects:

 Efficient Architecture:
Node.js utilizes an event-driven, non-blocking I/O model with a single-threaded event
loop, providing efficiency in handling concurrent operations.

 Versatility with Express.js:


Express.js, a minimalist and flexible framework, simplifies web application
development by offering robust routing, middleware support, and seamless integration
with various templating engines.

 Database Connectivity:
Node.js seamlessly connects to databases using dedicated drivers or ORM libraries,
with popular options including MongoDB, MySQL, and PostgreSQL.

 Scalability and Performance:


Techniques such as asynchronous code execution, load balancing, clustering, and best
practices contribute to the scalability and performance of Node.js applications.

Department of Electrical Engineering, SKIT M&G, Page


Jaipur
References

1. https://nodejs.org/en/docs/

2. https://developer.mozilla.org/en-US/docs/Glossary/Node.js

3. https://docs.npmjs.com/

4. https://docs.mongodb.com/drivers/node/

Department of Electrical Engineering, SKIT M&G, Page


Jaipur

You might also like