Nodejs Web Application Notes Part 1
Nodejs Web Application Notes Part 1
js
Building Scalable and Efficient
Web Applications
04 05 06
HTTP Response Structure HTTP status code HTTP Response Body
07
HTTP vs HTTPS
01
Introduction
Introduction
● HTTP stands for HyperText Transfer Protocol.
● It is a protocol used to access the data on the World Wide Web (www).
● The HTTP protocol can be used to transfer the data in the form of plain text, hypertext, audio, video, and so on.
● An HTTP request is the way internet communications platforms such as web browsers ask for the
information they need to load a website.
02
Scenario on
HTTP protocol
structure
Scenario explain HTTP call Structure
http://ammascakehouse:1st main road/cakelist/redvelvet
1. A URL
2. An HTTP method
1. GET Method ⇒ To retrieve data from a server, No request body (parameters are passed via URL or query
strings).
GET /api/customers/123
2. POST Method ⇒ To send data to the server to create a new resource or submit data (e.g., form
submissions). Data is sent in the request body.
POST /api/customers/register
{
"name" : "John",
"email" : "john@gmail.com",
"password" : "asdf1234"
}
HTTP Methods - 2
1. PUT Method ⇒ To update an existing resource or create a resource if it doesn’t exist. Sends data in the
request body.
PUT /api/customers/123
{
"name" : "John",
"email" : "john@gmail.com",
"password" : "john@admin123"
}
2. DELETE Method ⇒ To delete a resource from the server. No request body is usually needed.
DELETE /api/customers/123
04
HTTP Response
Structure
HTTP Response Structure
An HTTP response is a structured communication from the server that provides the client with the requested data
or an indication of the request's outcome.
The combination of the status line, headers, and body ensures that the client knows how to interpret and use the
response.
GET /api/cakes/list
HTTP Status Line
HTTP/1.1 200 OK
Date: Wed, 16 Oct 2024 12:45:00 GMT
HTTP Header Line
Content-Type: text/html; charset=UTF-8
Content-Length: 138
[{ name:"Red velvet", stock:17 },
HTTP Response Object
{ name:"Black forest", stock:11 },
{ name:"Chocolate", stock:12 }]
05
HTTP Status
code
HTTP Status Code
After a request, the server responds with a status code that indicates the result of the operation. These are
categorized into five main classes:
1xx (Informational): The request was received, and the server is continuing the process (e.g., 100 Continue).
2xx (Success): The request was successfully received and processed (e.g., 200 OK).
3xx (Redirection): The client must take additional action to complete the request (e.g., 301 Moved Permanently).
4xx (Client Errors): The request contains bad syntax or cannot be fulfilled (e.g., 404 Not Found).
5xx (Server Errors): The server failed to fulfill a valid request (e.g., 500 Internal Server Error).
403 FORBIDDEN 404 NOT FOUND 405 METHOD NOT ALLOWED 413 PAYLOAD TOO LARGE
Note:- The body is typically sent only in successful (e.g., 200 OK) or informational (e.g., 201 Created)
HTTPS (HyperText Transfer Protocol Secure): A secure version of HTTP. It uses encryption (via TLS/SSL) to ensure
that data sent between the client and server is encrypted, providing privacy, security.
Performance Slightly faster as it doesn’t require the Slightly slower due to the overhead of
overhead of encryption and decryption. encrypting and decrypting data
Summary on HTTP
What is HTTP?
- HyperText Transfer Protocol, used for communication between client (browser) and server
01 02 03
Introduction Global Objects Default Modules
01
Introduction
Introduction
● Node.js provides a rich set of global objects and default modules that empower us to build scalable and efficient
applications.
● Default modules, known as core modules, are modules that come bundled with Node.js.
It provides access to various built-in functions, properties, and objects that are available everywhere in your
Node.js application.
/* index.js */ /* data.js */
// Declare a global variable
global.appName = 'apple'; require("./index.js");
console.log(global.appName); console.log(global.appName);
// Outputs: MyNodeApp // Outputs: MyNodeApp
Process Objects in detail
2. process
It provides information about the currently running Node.js process and offers control over it.
An object representing the Node.js process itself, providing information about the environment, memory usage,
and more.
console.log(process.argv);
2. env
An object containing the user environment variables at the time the process was started.
console.log(process.version); // 'v22.7.0'
4. platform
A string identifying the operating system platform the Node.js process is running on.
5. arch
A string identifying the processor architecture for which the Node.js binary was compiled.
2. error()
2. info()
2. clear()
const people = [
{ name: 'John', age: 30 },
{ name: 'Jane', age: 28 },
];
console.table(people); // Displays the array as a table
console.time('Timer');
console.log("This console message");
console.timeEnd('Timer'); // Outputs: Timer: <elapsed time>ms
__dirname & __filename properties
4. __dirname
__dirname is a global variable that contains the directory name of the current module file.
console.log(__dirname);
// Output :- /Users/sandeepjaiswal/Downloads/test-api
5. __filename
__filename is a global variable that contains the full path and filename of the current module file.
This is useful for logging or debugging purposes to see where the current script is located.
console.log(__dirname);
// Output :- /Users/sandeepjaiswal/Downloads/test-api/index.js
setTimeOut method in detail
6. Timer :- setTimeOut()
The setTimeout() function in JavaScript (including Node.js) is used to execute a function or a specified piece of code
after a set delay (in milliseconds).
setTimeout(() => {
console.log('This message will be logged after 2 seconds');
}, 2000); // 2000 milliseconds = 2 seconds
setInterval method in detail
7. Timer :- setInterval()
The setInterval() is a JavaScript function used to repeatedly call a function or execute a block of code at specified
time intervals (in milliseconds)
function: The function or block of code that you want to run repeatedly.
delay: The time, in milliseconds, after which the function should be executed. 1000 milliseconds equals 1 second.
arg1, arg2, ...: Optional arguments passed to the function when it is called.
setInterval(() => {
console.log("This message will repeat every 2 seconds");
}, 2000); // 2000 milliseconds = 2 seconds
setImmediate method in detail
8. Timer :- setImmediate()
In Node.js, setImmediate() is a function that schedules a callback function to be executed immediately after the
current event loop cycle finishes.
It's similar to setTimeout() with a 0 millisecond delay, but setImmediate() is more efficient
callback: The function you want to execute after the current event loop finishes.
arg1, arg2, ...: Optional arguments that will be passed to the callback function when it is invoked.
console.log('Start');
setImmediate(() => {
console.log('This runs after the current event loop');
});
console.log('End');
Summary on Global Objects
Node.js has several global objects that are available in all modules without the need to require() them.
global
- Equivalent to window in browsers, it is the global scope object in Node.js.
process
- Provides information and control over the current Node.js process
Common properties and methods include:
console
- it provides methods to output messages to the terminal.
1. Builtin Modules:
Node.js comes with a set of built-in modules that you can use without installing any additional packages
fs path http https url util events
stream os crypto querystring zlib dns net
2. User-defined Modules
You can create your own modules by organizing your code into separate JavaScript files.
// index.js // data.js
const greet = (name) => `Hello, ${name}!`; const indexMod = require("./index");
module.exports = { greet }; console.log(indexMod.greet("Sandeep")); // Output: Hello, Sandeep!
● express: A web application framework for building web and mobile applications.
● mongoose: An ODM (Object Data Modeling) library for MongoDB and Node.js.
01
File System ( fs )
fs module in detail
1. fs
The fs (File System) module in Node.js allows you to interact with the file system.
Its helps us to interact file in both way, synchronous and asynchronous way.
It provides a variety of methods to read, write, update, delete, and manipulate files and directories.
Syntax:-
fs.readFileSync(path, [options])
Parameters:
2. options (Optional):
○ Can be either an object or a string. It determines the encoding and the mode used when reading the file.
○ Encoding: If not specified, it will return the raw buffer data. If specified as 'utf8', 'ascii', or other encodings,
the method returns a string.
List of fs methods in detail
1. Reading file with Encoding
The fs.readFileSync() method reads the contents of a file in a synchronous manner and logs the content as a
string (using 'utf8' encoding).
// index.js // abc.txt
const fs = require("fs"); This is the content.
2. options (Optional):
○ Encoding: If not specified, it will return the raw buffer data. If specified as 'utf8', 'ascii', or other encodings,
the method returns a string.
3. callback:
● The callback function is called after the file has been read. It takes two parameters:
○ err: Error object if there was an error reading the file (e.g., if the file doesn't exist).
○ data: The data read from the file, either as a string (if encoding is provided) or as a Buffer (if no encoding is
specified).
List of fs methods in detail
Reading file with Encoding
In this example, we are reading the contents of a file asynchronously and providing a callback to handle the
result.
// index.js // abc.txt
const fs = require("fs"); This is the content.
Parameters:
This method does not return a value. It simply writes the data to the file and then allows the program to proceed. If there
is an error, it throws an exception.
const fs = require("fs");
// list.txt
Hello, World!
// Write a string to a file asynchronously
fs.writeFile("list.txt", "Hello, World!", (err) => {
if (err) {
console.error("Error writing file:", err);
return;
}
console.log("File written successfully");
});
List of fs methods in detail
4. appendFile()
The fs.appendFile() method in Node.js is used to asynchronously append data to a file.
If the file does not exist, it will be created.
fs.appendFile('./list.txt', '\nHow are you \n', (err) => { How are you
if (err) {
console.error('Error appending to file:', err);
return;
}
console.log('Data appended to file successfully');
});
List of fs methods in detail
4. unlink()
The fs.unlink() method in Node.js is used to asynchronously delete a file or symbolic link.
If the file does not exist, it will be created.
const fs = require("fs");
// Delete a file asynchronously
// OutPut
fs.unlink("data.txt", (err) => {
File deleted successfully
if (err) {
console.error("Error deleting the file:", err);
return;
}
console.log("File deleted successfully");
});
If file.txt exists, it will be deleted. If it doesn't exist, the err object will contain details about the error.
List of fs methods in detail
4. mkdir()
The fs.mkdir() method in Node.js is used to asynchronously create a new directory.
const fs = require("fs");
const fs = require("fs");
1. SSL/TLS: HTTPS uses SSL (Secure Sockets Layer) or its successor TLS (Transport Layer Security) to encrypt data
transferred between the client and the server, ensuring confidentiality and security.
2. Certificates: The server must have an SSL/TLS certificate to establish a secure connection. These certificates are
typically issued by a trusted Certificate Authority (CA), but you can also use self-signed certificates for development
purposes.
Https module in detail
An HTTPS server is created similarly to an HTTP server, but it requires an additional layer of encryption through SSL/TLS.
You need to provide:
These can either be generated for testing (self-signed certificates) or obtained from a Certificate Authority for production.
1. Generate SSL/TLS certificates (for development, you can generate a self-signed certificate using OpenSSL).
2. Use the https.createServer() method, providing the SSL key and certificate, and a request listener.
Https module in detail
This module provides utilities for parsing and formatting URL query strings, converting them to and from objects,
and handling encoding/decoding of special characters.
stringify() parse() encode() decode()
// Importing the querystring module
const querystring = require('querystring');
const obj = { name: "John Doe", age: 30, city: "New York"};
01 02 03
Introduction Key Features Package Installation
01
Introduction
Introduction
The npm (Node Package Manager) is a powerful tool that comes bundled with Node.js.
It is the default package manager for JavaScript and is widely used in Node.js applications to manage libraries,
frameworks, and other tools.
It provides a large online registry (https://www.npmjs.com/) where developers can publish and discover
packages.
Key Features:-
This process is essential for organizing dependencies and maintaining project structure.
It provides a large online registry (https://www.npmjs.com/) where developers can publish and discover
packages.
Initializing a Project
To start using npm for package management, you need to initialize your project.
This creates a package.json file that tracks your project's dependencies.
npm init
Steps 1:- To verify the Node js installation using the following commands in the terminal:
node -v
npm -v
Steps 2:- Create a new directory for your project using following command or create new folder in any
drive in Windows OS:
mkdir my-node-project
cd my-node-project
npm init
Package Management using npm in detail
Initialize the Project with package.json
// package.json
This installs the package in the node_modules directory and adds it as a dependency in package.json.
// package.json
To install a package globally (available for all projects), use the -g flag:
{
npm install -g <package-name>
"name": "test-api",
"version": "1.0.0",
This installs the package globally, allowing you to use it in any Node.js project.
"dependencies": {
"express": "^4.21.1"
Folder Structure:-
Example:- }
test-api }
├── package.json
npm install express
├── node_modules
This installs the Express framework locally.
Package Management using npm in detail
Manage Package Dependencies :-
The package.json file plays a crucial role in managing dependencies.
{
Using Scripts :-
"name": "test-api",
You can define scripts in package.json to automate tasks related to your project. "scripts": {
Common scripts include starting the application, running tests, and building the project. "start": "node index.js",
"dev": "nodemon index.js",
Run scripts using: "test": "jest"
},
npm run <script-name> eg:- npm run dev
}
Package Management using npm in detail
Versioning Package :-
Versioning in npm follows Semantic Versioning (SemVer), which uses the format
MAJOR.MINOR.PATCH: // package.json
● Minimum Version: npm install <package-name>@^1.2.3 (accepts any version >=1.2.3 <2.0.0)
● Latest Version: npm install <package-name>@latest
03
Key Feature
- Version Control
Version Control using npm in detail
Version control in npm revolves around managing package versions in a consistent and structured way.
It ensures that projects can reliably install the correct versions of dependencies and avoid compatibility issues.
The npm adopts Semantic Versioning, which follows a format of MAJOR.MINOR.PATCH (e.g., 1.2.3).
This file ensures exact version control by locking the dependency tree so that the project can always install the exact
same versions of every package.
● Locks exact versions: Ensures that when the project is installed on another machine, it uses the same versions of
all dependencies, even indirect dependencies.
● Improves install times: Speeds up npm install by caching exact package versions.
● Ensures consistency: Prevents version drift between different environments (e.g., development vs. production).
Example:-
npm install express@4.16.0
If you want to update to the latest major version, you can install it explicitly:
npm outdated
This command will show you a list of all packages that have newer versions available than the ones currently
installed.
When managing packages, you might want to address security vulnerabilities. npm provides an audit feature to
help with this:
npm audit
This command checks for vulnerabilities in your dependencies. To automatically fix these vulnerabilities, you can
run:
You can chain multiple commands in a single script using && (runs the next command if the previous one succeeds)
or || (runs the next command if the previous one fails).
{
"scripts": {
"test-and-lint": "npm run test && npm run lint"
}
}
Script using npm in detail
Environment variables can be defined within npm scripts, allowing different configurations for each environment.
{
"scripts": {
"start": "NODE_ENV=production node server.js",
"dev": "NODE_ENV=development nodemon server.js"
}
}
In the start script, NODE_ENV is set to production, while in dev, it is set to development.
On Windows, you may need to install the cross-env package to set environment variables:
{
"scripts": {
"start": "cross-env NODE_ENV=production node server.js"
}
}
Script using npm in detail
Example of a package.json with Advanced npm Scripts:-
{
"name": "my-app",
"version": "1.0.0",
"scripts": {
"start": "node server.js",
"build": "webpack --config webpack.config.js",
"test": "jest",
"lint": "eslint src/**/*.js",
"dev": "cross-env NODE_ENV=development nodemon server.js",
"prebuild": "echo 'Preparing to build...'",
"prestart": "npm run build",
"test-and-lint": "npm run test && npm run lint",
"compress": "zip -r dist.zip dist/",
"deploy": "npm run build && npm run compress"
}
}
Node.js
Server setup using HTTP
REQ RES
01 02 03
API Introduction Server setup
04
Application Programming
Interface ( API )
Application Programming Interface in detail
An API (Application Programming Interface) is a set of rules and protocols that allows different software
applications to communicate with each other.
It acts as an intermediary between two applications, enabling them to exchange data and execute predefined
actions
Application Programming Interface in detail
Components of an API
● Request: The request is made by a client (e.g., a web app, mobile app) to the server where the API is
hosted. It typically includes:
○ HTTP Method: Defines the action to perform (e.g., GET, POST, PUT, DELETE).
○ Headers: Provide metadata for the request (like authorization, content type).
○ Path/Endpoint: Identifies the specific resource (e.g., /user/123).
○ Parameters: Provide additional options for the request, such as filters, search terms, or limits.
● Response: The server responds to the client’s request with data or an acknowledgment:
○ Status Code: Tells the client the outcome (e.g., 200 for success, 404 for not found, 500 for server
error).
○ Body: Contains the data, often in JSON format, if the request is successful.
Application Programming Interface in detail
Popular API Protocols
● Uses XML for message format and can operate over various protocols.
● Provides a higher level of security (WS-Security) and transaction control, making it popular in financial and
enterprise applications.
Application Programming Interface in detail
Difference between REST and SOAP API.
Architectural style based on HTTP. Strict protocol with specific rules and standards.
Supports JSON, XML, HTML, and plain text (JSON is Uses only XML for message formatting.
most common).
Stateless communication, with each request being Can be stateless or stateful, allowing for more
independent. complex interactions.
Generally faster, lighter, and more efficient for web Slower due to XML’s verbosity and complexity,
use due to lightweight data formats. resulting in larger payloads.
Uses standard HTTP status codes (e.g., 404 for Not Uses specific error codes within the XML response,
Found, 500 for Server Error). such as <faultcode> and <faultstring>.
Preferred for web and mobile applications due to its Often used in enterprise applications where high
simplicity and flexibility. security and transaction management are required.
05
Introduction to
Express JS
GET Request handled for server
/redVelvetCake
{
quantity: 5, /redVelvetCake
weight:'500 gm' REQ
/pastries
}
/candles
Waiter/API
/redVelvet cake showing the
/pastries list of item
/candles (req,res) => {
RES // Prepare cake and send
}
Introduction to Express.js
Express.js is a flexible, minimalist web application framework for Node.js
It provides a streamlined approach to handling HTTP requests and responses, routing, middleware, and
templating.
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
Explaining The GET API for /redVelvetCake
/redVelvetCake {
Req Object quantity: 5,
weight: '500 gm'
}
{ req.body -
quantity: 5, req.url - /redVelvetCake
weight:'500 gm'
}
Routes Name
});
Request Data
Connection among Systems
Client DataBase
Server
HTML/CSS/ Express.JS MongoDB
Javascript
Or
POSTMAN
06
Request & Response
Objects
Passing the data from client to server
http://localhost:3000/api/cakelist/
http://localhost:3000/api/cakelist/:cakeType
Amma’s Cake House
http://localhost:3000/api/cakelist/:cakeType?
weight=500gm
Client
HTML/CSS/ { Server
firstName:"John",
Javascript Express.JS
Or lastName:"Jaiswal",
POSTMAN password:"asdf1234",
}
Request Object in detail
The req object represents the HTTP request and has properties for the request query string, parameters, body,
HTTP headers, etc.
Url:- http://localhost:3000/api/cakelist/:cakeType?weight=500gm
Headers:{ ‘Content-Type’:’application/json’}
Body:{ ‘name’:’John’}
Request Object in detail
Any Request will have such elements :-
URL : http://localhost:3000/api/cakelist
Body data:- {
firstName:"John",
lastName:"Jaiswal",
password:"asdf1234",
}
Request Object in detail
http://localhost:3000/api/cakelist/:cakeType?weight=500gm
res.json(): Sends a JSON response. Automatically sets the Content-Type header to application/json.
To parse JSON data, use the express.json() middleware. This is useful when the client sends data in JSON format,
which is typical for APIs.
In MongoDB,
MongoDB stores data in JSON-like documents with dynamic schemas, which means you can store and retrieve
data without having to define a strict structure in advance.
Core concepts:
Create Database
config 60.00 KiB
data-db 208.00 KiB
Use command to list all the database list db-test 72.00 KiB
enhanced_authentication-db 144.00 KiB
# mongosh flow-proximity-db 144.00 KiB
local 184.00 KiB
>show dbs
MongoDB Create Database
Creating a database in MongoDB using the Mongo shell is straightforward.
Instead, a database is created automatically when you insert data into it for the first time.
Before creating a database, make sure you have MongoDB installed on your system and that the MongoDB
server (mongod) is running.
# mongosh
Before creating a new database, let’s check which databases already exist on your MongoDB server.
# mongosh
Before creating a new database, let’s check which databases already exist on your MongoDB server.
In MongoDB, data is stored in documents, which are JSON-like objects. These documents are stored in collections
Syntax :-
Examples:-
db.products.insertMany([
{ name: "Laptop", price: 1200, category: "Electronics" },
{ name: "Headphones", price: 150, category: "Accessories" }
])
{
Output:- acknowledged: true,
insertedIds: {
"0": ObjectId("64fc2f8d9abf7c8b7c12d570"),
"1": ObjectId("64fc2f8d9abf7c8b7c12d571")
}
}
MongoDB read operation to database
The Read operation in MongoDB involves retrieving documents from a collection
In MongoDB, this is done using methods like find() and findOne() methods.
MongoDB offers a rich set of query capabilities that allow you to filter, sort, and project data to get the exact information
you need.
The find() method retrieves multiple documents from a collection that match a specified query.
The findOne() method retrieves a single document that matches the query.
Syntax:-
db.collection.find(query, projection)
db.users.find({}, { _id: 0 })
Here above _id is ignored in response since _id:0
Node.js
Mongodb CRUD(read/update) operation
Example:-
db.users.find({
$or: [{ age: { $lt: 20 } }, { status: "inactive" }]
})
db.users.find({
$and: [{ age: { $gt: 25 } },{ status: "active" }]
})
3. If your document has nested fields, use dot notation. For example, to find documents where the address.city is
"Mumbai":
db.users.find().limit(5)
db.users.find().sort({ age: 1 })
MongoDB provides several methods to update documents, including updating specific fields, replacing entire
documents, and performing bulk updates.
Syntax:-
Update the status field of the first document where name is "Sandeep":
The updateMany() method updates all documents that match the filter. Useful for bulk updates.
Syntax:-
The replaceOne() method replaces the entire document that matches the filter with a new document.
The document structure will be completely replaced, except for the _id field.
db.users.replaceOne(
{ name: "Sandeep" },
{ name: "Sandeep", age: 30, status: "active", department: "Engineering" }
);
MongoDB Update operators in detail
let’s review some important update operators:
Operator Description
$set Sets the value of a field. Adds the field if it does not exist.
$unset Removes the specified field from a document.
$inc Increments the value of a field by a specified amount.
$mul Multiplies the value of a field by a specified factor.
$rename Renames a field.
$min Updates the field if the specified value is less than the current value.
$max Updates the field if the specified value is greater than the current value.
$push Appends a value to an array.
$pop Removes the first or last element from an array.
$addToSet Adds a value to an array only if it doesn’t already exist.
$pull Removes all instances of a value from an array.
MongoDB Update operators example
$inc Increments the value of a field by a specified amount.
$min Updates the field if the specified value is less than the current value.
Let's update the student's score to 75 if the current score is greater than 75.
The deleteOne() method deletes one document that matches the filter criteria. If multiple documents match, only the
first document found will be deleted.
Syntax:- Example:-
The findOneAndDelete() method finds a single document, deletes it, and returns the deleted document.
It is useful when you need to remove a document and use its data for further processing.
Syntax:- Example: Find and delete a user with name:"Sandeep", and return the deleted document:
db.collection.findOneAndDelete(filter, options) db.users.findOneAndDelete({ name: "Sandeep" });
Options:
● sort: Sorts the documents before deleting, so you can control which document gets deleted if multiple documents match.
● projection: Specifies the fields to return in the deleted document.
MongoDB Delete operation with example
How to Delete All Documents in a Collection
If you want to delete all documents in a collection but keep the collection itself, use an empty filter with deleteMany():
db.users.deleteMany({});
db.dropDatabase();