0% found this document useful (0 votes)
39 views9 pages

NODE JS

Uploaded by

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

NODE JS

Uploaded by

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

Node JS

1. Node JS
2. Introduction
3. Understanding the framework
4. Setting up the Environment
5. Node PackageManager
6. AngularJS & NodeJS
7. HTTP Protocol
8. Building HTTP server
9. File Handling withNodeJs- Buffers - Streams - Event-
Modules
10. Express Web Framework
11. Database Handling with NodeJS
Node JS- Introduction and Understanding
framework
Node.js is a JavaScript runtime environment that allows developers to run
JavaScript code on the server side, outside of a browser. Created in 2009,
Node.js is built on the V8 JavaScript engine (the same engine that powers
Google Chrome), and it enables developers to use JavaScript to build
server-side applications and back-end systems.
● Single Language for Full Stack: With Node.js, developers can use JavaScript
for both front-end and back-end development, making it easier to maintain
and write code across the entire application.
● Fast and Lightweight: Node.js is lightweight because it does not block
threads while handling tasks. It is built on the V8 JavaScript engine, which
optimizes code execution speed.
● Node.js is a runtime environment that lets developers run JavaScript on the
server side, outside of a browser. It is built on the V8 JavaScript engine
(used in Chrome) and is designed for building fast, scalable server-side
applications.
● Node.js comes with npm (Node Package Manager), which provides a huge
library of open-source packages that can speed up development and add
functionality, such as Express.js for creating web servers.

Setting up the node js


● Download and install Node.js from nodejs.org.
● To create a simple HTTP server that listens on port 3000 and responds with
"Hello, World!"
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello, World!');
});
server.listen(3000, () => {
console.log('Server is running at
http://localhost:3000');
});

● Run the code in the terminal with command node server.js

Node PackageManager
The Node Package Manager (npm) is a tool that comes with Node.js, used to
manage and share JavaScript packages or modules. It simplifies the process of
adding, updating, and removing libraries or tools needed for a Node.js project.
Features:
● Package Management: npm allows developers to easily install packages
(pre-written code modules) from a large public registry. This saves time by
reusing code that others have already written and tested.
● Dependency Management: npm keeps track of all the dependencies (other
packages) required for a project. It automatically installs these
dependencies, so developers don’t have to manually download and manage
each one.
● Scripts Automation: npm can be used to define scripts that automate
repetitive tasks, such as testing, building, or running a server.
● The package.json file is automatically generated by npm when
initializing a project. It contains metadata about the project

Common npm commands:


● npm install <package-name> : Installing Packages
● npm install -g <package-name> : Global Installation
● npm init : Initializing a Project
● npm uninstall <package-name> : Removing Packages
Angular js
AngularJS is a front-end JavaScript framework developed by Google. It is
primarily used to build dynamic, single-page applications (SPAs) that run in
the browser.

AngularJS is designed to make it easier to build complex, data-driven user


interfaces. It allows developers to use HTML as a template language and
extend HTML’s syntax to express application components.

AngularJS is used to create the front end of an application, managing how


data is displayed to users and how users interact with it. It is commonly used
to build applications that require a dynamic user interface, such as
dashboards or content management systems

Node js
Node.js is a runtime environment for executing JavaScript code on the server
side. It was created by Ryan Dahl in 2009 and enables JavaScript to be used
outside of a browser.

Node.js is used for building scalable, high-performance server-side


applications. It allows JavaScript to be used for server-side development,
enabling a single language (JavaScript) to be used across both the front end
and back end.

Node.js is used to create the back end of an application. It is commonly used


for building APIs, handling data requests, and managing application logic and
database interactions.
HTTP Protocol
Definition:
HTTP (Hypertext Transfer Protocol) is the standard protocol used to transfer
data (e.g., web pages, images, videos, and API responses) over the internet
between clients (browsers) and servers.
Purpose:
It enables communication in a request-response model where the client sends
an HTTP request, and the server processes it and sends back a response.

HTTP Methods:
● GET: Fetch data (e.g., retrieving a web page).
● POST: Send data (e.g., form submissions).
● PUT: Update existing resources.
● DELETE: Remove resources.

Building HTTP server


● Install Node.js from the official website and verify the installation
using the terminal.
● Create a new folder for the project and navigate into it using the
terminal.
● (Optional) Initialize the project using npm init to create a
package.json file for managing dependencies.
● Use the built-in http module of Node.js to create the server.
● Define the server behavior to listen for client requests and send
appropriate responses.
● Set the server to listen on a specific port, such as 3000, so it can
accept incoming requests.
● Save the file with a .js extension, such as server.js.
● Open a terminal in the project directory and run the server using the
command node server.js.
● Open a browser and navigate to http://localhost:<port> (e.g.,
http://localhost:3000) to test the server.
● Verify that the server responds to requests with the desired output or
error messages.

File Handling with Node.js


Node.js provides built-in modules like File System to perform file operations
such as reading, writing, updating, and deleting files. It supports both
synchronous (blocking) and asynchronous (non-blocking) methods.

Buffers:
● Buffers are temporary storage spaces in memory used to handle binary
data.
● They are particularly useful when reading or writing large files or
streaming data, as the data is broken into chunks.
● Buffers provide methods to manipulate raw data efficiently, such as
converting it to strings or slicing it.

Streams:
● Streams in Node.js are objects that allow the reading or writing of data
sequentially and are memory-efficient and ideal for handling large
amounts of data.

Types of streams:

● Readable Streams: Used to read data (e.g., from files or network


requests).
● Writable Streams: Used to write data (e.g., to files or responses).
● Duplex Streams: Can read and write data (e.g., network sockets).
● Transform Streams: Modify or transform data as it is read or written
(e.g., compression).

Events:
● Node.js is event-driven, meaning it listens for and responds to events.
● The events module provides an EventEmitter class, which is used to
manage custom events.
● Common use: Triggers actions (like reading a file) when a specific event
occurs.
● Example: Emitting an event when a file is read completely.

Modules:
● Modules in Node.js are reusable blocks of code which makes code
manageable, reusable, and easier to maintain.
● The require function is used to import modules into a program.

Types of modules:

● Core Modules: Built into Node.js (e.g., fs, http, path).


● Local Modules: Custom modules created by the user.
● Third-Party Modules: Installed via npm (e.g., express).

Express Web Framework


Express is a fast, minimal, and flexible web framework built on Node.js. It
simplifies the creation of web applications and APIs by providing essential
tools and features.

It is designed to handle the backend (server-side) logic for web applications


and APIs efficiently.
Advantages:
● Simplifies server setup compared to raw Node.js.
● Flexible and highly customizable.
● Large community and rich ecosystem of middleware libraries.
● Efficient handling of HTTP methods like GET, POST, PUT, DELETE.

Key Features:
● Routing: Allows you to define URL paths (routes) and the logic to handle
requests for those paths.
● Middleware Support: Provides functions that execute during the
request-response cycle, such as logging, authentication, or error
handling.
● Template Engines: Supports rendering dynamic HTML pages using
template engines like EJS, Pug, or Handlebars.
● RESTful API Development: Simplifies building REST APIs to interact with
databases and clients.
● Integration: Easily integrates with databases like MongoDB, MySQL, and
third-party tools.

Database handling with node


Purpose: Connect Node.js applications to databases (SQL or NoSQL) to
perform CRUD (Create, Read, Update, Delete) operations.
Supported Databases:
● SQL: MySQL, PostgreSQL, SQLite.
● NoSQL: MongoDB, CouchDB, Firebase.
Steps:
● Install the required database driver via npm (e.g., mysql2 for MySQL,
mongodb for MongoDB).
● Establish a connection using the database credentials.
● Perform operations like querying, inserting, updating, or deleting data.
● Close the connection after operations are complete.

Advantages:
● Asynchronous operations allow handling multiple queries efficiently.
● JavaScript is used both for server-side logic and database interaction.

Popular Libraries:
● SQL: mysql2, pg (PostgreSQL), Sequelize (ORM).
● NoSQL: mongodb, Mongoose (ODM).

Use Cases:
● Building APIs for web or mobile applications.
● Managing real-time data in dynamic applications.

You might also like