NodeJs + ExpressJs + MongoDB-notes
NodeJs + ExpressJs + MongoDB-notes
Course Notes
Contains 1 PDF lessons
KnowledgeGate
Module
Nodejs
Topic
NodeJS
Subtopic
NodeJS
Lesson Lesson 1 of 1
NodeJs_Notes
Playlist Link
NodeJS
Complete Course
Introduction to NodeJS
1. Pre-requisites
2. What is NodeJS
3. NodeJs Features
4. JavaScript on Client
5. JavaScript on Server
6. Client Code vs Server Code
7. Other uses of NodeJs
8. Server architecture with NodeJs
JS is required for NodeJS
React is recommended before NodeJS
[Link] is NodeJS
1. File System Access: Provides APIs to read and write files directly,
which is not possible in browser environments for security
reasons.
2. Server-Side Capabilities: [Link] enables JavaScript to run on the
server, handling HTTP requests, file operations, and other
server-side functionalities.
3. Modules: Organize code into reusable modules using require().
3. NodeJs Features
(Removed)
1. P•Crlicek
•Second level -tor eediqt Muaistseri tteexts styles
•is NodeJS
•Third level
2. Wha•tFo urth level
Fifth level
3. NodeJs Features
4. JavaScript on Client
5. JavaScript on Server
6. Client Code vs Server Code
7. Other uses of NodeJs
8. Server architecture with NodeJs
2. Installation of NodeJS
1. What is IDE
2. Need of IDE
3. MAC Setup
• Install latest Node & VsCode
4.
Windows Setup
5. •Install latest Node & VsCode
Linux Setup
6. •Install latest Node & VsCode
7. VsCode (Extensions and Settings)
8. Executing first .js file
9. What is REPL
Executing Code via REPL
2.1 What is IDE
1. IDE stands for Integrated
Development Environment.
2. Software suite that
consolidates basic tools
required for software
development.
3. Central hub for coding,
finding problems, and testing.
4. Designed to improve
developer efficiency.
2.2 Need of IDE
1. Streamlines development.
2. Increases productivity.
3. Simplifies complex tasks.
4. Offers a unified
workspace.
5. IDE Features
1. Code Autocomplete
2. Syntax Highlighting
3.
4.
Version Control
Error Checking
2.3 MAC Setup
•Click to edit Master text styles
• Second level
• Third level
• Fourth level
•Fifth level
2.3 MAC Setup
(Install latest Node)
Search Download NodeJS
2.3 MAC Setup
(Install VsCode)
Se•aCrlicckh
•Second level
t oV eSd osgle
itC Moadsteer otenxt Gstyole
•Third level
• Fourth level
•Fifth level
2.4 Windows Setup
•Click to edit Master text styles
• Second level
• Third level
• Fourth level
•Fifth level
2.4 Windows Setup
(Install latest Node)
Search Download NodeJS
2.4 Windows Setup
(Install VsCode)
Se•aCrlickh t oV eSd itC Moadster otenxt Gstyoleosgle
•Second level
•Third level
• Fourth level
•Fifth level
2.5 Linux Setup
2.5 Linux Setup
(Install latest Node)
Search Download NodeJS
2.5 Linux Setup
(Install VsCode)
Se•aCrlickh t oV eSd itC Moadster otenxt Gstyoleosgle
•Second level
•Third level
• Fourth level
•Fifth level
2.6 VsCode
(Extensions and Settings)
1. What isedit
•Click to IDE Master text styles
2. NeeSdec
•
oonfd leIvDel
•ThirdlevelE
3. •uFpifthlevel
MAC S•eFtourth level
• Install latest Node & VsCode
4.
Windows Setup
5. •Install latest Node & VsCode
Linux Setup
6. •Install latest Node & VsCode
7. VsCode (Extensions and Settings)
8. Executing first .js file
9. What is REPL
Executing Code via REPL
3. First Node Server
[Link] DNS Works?
[Link] Web Works?
[Link] are Protocols?
[Link] Core Modules
5. Require Keyword
[Link] first Node Server
3.1 How DNS Works?
[Link] Name Entry: User types a domain (e.g.,
[Link]) into the browser.
[Link] Query: The browser sends a DNS query to resolve
the domain into an IP address.
3. DNS Server: Provides the correct IP address for the
domain.
[Link] Connects: The browser uses the IP to connect
to the web server and loads the website.
3.1 How DNS Actually Works?
1. Root DNS: Acts as the starting point for DNS resolution. It
directs queries to the correct TLD server (e.g., .com, .org).
[Link] (Top-Level Domain) DNS: Handles queries for specific
top-level domains (e.g., .com, .net) and directs them to the
authoritative DNS server (e.g., Verisign for .com, PIR for .org)
3. Authoritative DNS: Contains the actual IP address of the
domain and answers DNS queries with this information.(e.g.,
Cloudflare, Google DNS).
3.2 How Web Works?
[Link] Request Initiation: The client (browser) initiates a
network call by entering a URL.
[Link] Resolution: The browser contacts a DNS server to
get the IP address of the domain.
[Link] Connection: The browser establishes a TCP
connection with the server's IP address.
[Link] Request: The browser sends an HTTP request to
the server.
5. Server Processing: The server processes the request and
6. prepares a response.
HTTP Response: The server sends an HTTP response back
to the client.
[Link] Transmission: The response travels back to the
client over the network.
[Link] Receives Response: The browser receives and
interprets the response.
[Link]: The browser renders the content of the
response and displays it to the user.
3.3 What are Protocols?
Http (HyperText Transfer Protocol):
●Facilitates communication between a web browser and a server to
transfer web pages.
● Sends data in plain text (no encryption).
● Used for basic website browsing without security.
HTTPS (HyperText Transfer Protocol Secure):
●Secure version of HTTP, encrypts data for secure communication.
● Uses SSL/TLS to encrypt data.
●Used in online banking, e-commerce.
TCP (Transmission Control Protocol):
● Ensures reliable, ordered, and error-checked data delivery over the
● internet.
Establishes a connection before data is transferred.
3.4 Node Core Modules
[Link]-in: Core modules are included
with [Link] installation.
[Link] Installation Needed: Directly
available for use without npm
install.
[Link]: Highly optimized for
performance.
3.4 Node Core Modules
[Link] (File System): Handles file operations like reading and
writing files.
[Link]: Creates HTTP servers and makes HTTP requests.
[Link]: Launch a SSL Server.
[Link]: Provides utilities for handling and transforming file
[Link]: Provides operating system-related utility methods
and properties.
[Link]: Handles events and event-driven programming.
[Link]: Provides cryptographic functionalities like hashing and
encryption.
[Link]: Parses and formats URL strings.
3.5 Require Keyword
[Link]: Imports modules in [Link]. Syntax:
[Link]: Modules are cached after the const moduleName = require('module’);
first require call.
3..js is added automatically and is not
needed to at the end of module name.
[Link] Resolution: [Link] searches for
modules in core, node_modules, and file
paths.
3.6 Creating first Node Server
3.6 Creating first Node Server
3.6 Creating first Node Server
[Link]
4.3 Understand Request Object
1. Streams
2. Chunks
3. Buffers
[Link] Chunk
[Link] Chunks
[Link] Request
[Link] Modules
5.1 Streams
5.1 Streams
5.2 Chunks
5.2 Chunks
5.3 Buffers
5.4 Reading Chunk
5.5 Buffering Chunks
5.6 Parsing Request
5.6 Parsing Request
5.7 Using Modules
5.7 Using Modules
Practise Set
Create a Calculator
1. Create a new [Link] project named “Calculator”.
2. On the home page (route “/”), show a welcome message and a
3. link to the calculator page.
4. On the “/calculator” page, display a form with two input fields
and a “Sum” button.
When the user clicks the “Sum” button, they should be taken to
the “/calculate-result” page, which shows the sum of the two
numbers.
○ Make sure the request goes to the server.
○ Create a separate module for the addition function.
○ Create another module to handle incoming requests.
○ On the “/calculate-result” page, parse the user input, use
the addition module to calculate the sum, and display the
result on a new HTML page.
Revision
•Click to edit Master text styles
1.S•Stecroned alevmels
•Third level
[Link]•Fokurtsh level
•Fifth level
3. Buffers
[Link] Chunk
[Link] Chunks
[Link] Request
[Link] Modules
6. Event Loop
[Link] Driven
[Link] Threaded
3.V8 vs libuv
[Link] Runtime
[Link] Loop
[Link] Code
[Link] Code
6.1 Event Driven
6.2 Single Threaded
6.3 V8 vs libuv
V8:
[Link]-source JavaScript engine by Google.
[Link] in Chrome and [Link].
[Link] JavaScript to native machine
code.
[Link] high-performance JavaScript
execution.
libuv:
[Link]-platform support library for [Link].
[Link] asynchronous I/O operations.
[Link] event-driven architecture.
[Link] file system, networking, and
timers non-blockingly across platforms.
6.4 Node Runtime
6.4 Node Runtime
6.4 Node Runtime
6.4 Node Runtime
6.5 Event Loop
●timers: this phase executes callbacks
scheduled by setTimeout() and
setInterval().
●pending callbacks: executes I/O callbacks
deferred to the next loop iteration.
● idle, prepare: only used internally.
●poll: retrieve new I/O events; execute I/O
related callbacks (almost all with the exception
of close callbacks, the ones scheduled by
timers, and setImmediate()); node will block
here when appropriate. check: setImmediate()
● callbacks are invoked here.
// Timer queue
setTimeout(() => [Link]('3. Timer 1'), 0);
// I/O queue
const fs = require('fs');
[Link]('[Link]', () => [Link]('4. I/O operation'));
// Check queue
setImmediate(() => [Link]('5. Immediate 1'));
// Close queue
[Link]('exit', (code) =>
{ [Link]('6. Exit
event'); });
1.•CElick
•Sveceond
to edit
ltev
Master
eDlriven
text styles
•Third level
[Link]•Flouerth
•Fi fthTlevel
levhelreaded
3.V8 vs libuv
[Link] Runtime
[Link] Loop
[Link] Code
[Link] Code
7. NPM & Tools
npm install
Recreates node_modules
7.7 Using nodemon
•Third levelaterial
1.I•nSescotnda llelv eMl Icons
•Fourth level
[Link] of Errors
[Link] Errors
[Link] Errors
4. Logical Errors
[Link] the Debugger
[Link] with Async Code
[Link] Debug with nodemon
8.1 Types of Errors
Step 2
8.5 Using the Debugger
Step 4: Use the tools Step 6: Debug Panel
Step 5: Hover
8.5 Using the Debugger
Step 7: Using Debug Console
8.6 Debugger with Async Code
8.7 Restart Debug with nodemon
Practise Set
Debug and fix Syntax, Runtime and Logical Errors
function calculateArea(width, height {
return width + height;
}
[Link] Errors
4. Logical Errors
[Link] the Debugger
[Link] with Async Code
[Link] Debug with nodemon
9. Starting with [Link]
[Link] is [Link]
[Link] of [Link]
[Link] [Link]
[Link] Middleware
[Link] Response
[Link] DeepDive
[Link] Routes
9.1 What is [Link]
[Link] is a minimal and flexible
web application framework for
[Link].
[Link] provides a robust set of features
for building single-page,
multi-page, and hybrid web
applications.
[Link] simplifies server-side
coding by providing a layer of
fundamental web application
features.
9.2 Need of [Link]
[Link] Simplifies Server Creation: Helps in quickly
setting up and running a web server without the need for
complex coding.
[Link] Management: Provides a powerful routing
mechanism to handle URLs and HTTP methods
effectively.
[Link] Support: Allows the use of middleware to
handle requests, responses, and any middle operations,
making code modular and maintainable.
[Link] Development: Facilitates easy and efficient
creation of RESTful APIs.
[Link] and Plugins: Has a large ecosystem with
numerous plugins and extensions, accelerating
development time.
9.3 Installing [Link]
9.3 Installing [Link]
9.4 Adding Middleware
9.4 Adding Middleware
9.4 Adding Middleware
9.5 Sending Response
9.6 Express DeepDive
9.6 Express DeepDive
9.6 Express DeepDive
9.7 Handling Routes
9.7 Handling Routes
[Link] matters
[Link] not call next() after
send()
3.“/” matches everything
[Link] [Link] implicitly
calls [Link]()
9.7 Handling Routes
9.7 Handling Routes
Practise Set
•Second
1.•CWlick letve
hto lisMaster
aedit
•Third level
[Link]
text styles
•F iftohlevel
[Link]•Fdourth lefvel [Link]
[Link] [Link]
[Link] Middleware
[Link] Response
[Link] DeepDive
[Link] Routes
10. [Link] DeepDive
[Link] Requests
[Link] Router
[Link] 404
[Link] Paths
[Link] HTML Files
[Link] HTML Files
[Link] File Helper
10.1 Parsing Requests
10.2 Express Router
10.2 Express Router
10.2 Express Router
10.3 Adding 404
10.4 Common Paths
or
10.5 Adding HTML Files
10.6 Serving HTML Files
10.7 Using File Helper
Practise Set
•Secronsd
1.•CPlicak lienvelg
to edit
•Third level
Requests
Master text styles
[Link]•rFourth
•eFiftshlevel
lesvel Router
[Link] 404
[Link] Paths
[Link] HTML Files
[Link] HTML Files
[Link] File Helper
11. Styling using
1. Install:
npm init -y
npm install -D tailwindcss postcss autoprefixer
2. Initialize Tailwind CSS Config
npx tailwindcss init
11.6 Installing Tailwind CSS
Configure Tailwind in the Configuration Files ([Link])
11.6 Installing Tailwind CSS
8. Run Command
npm run tailwind
11.7 Using Tailwind CSS
11.8 Building Tailwind Automatically
Practise Set
•Scond tro
1.•CSlicek levnelg Static
evdiit
•Third level
MasterFiles
text styles
•Fidfthlevel
[Link]•rFoourth luevecltion to Tailwind CSS
[Link] Classes
[Link] Extension
[Link] Tailwind CSS
[Link] Tailwind CSS
[Link] Tailwind CSS
12. Dynamic UI using EJS
[Link] of Dynamic UI
[Link] using Global Variables
[Link] is EJS
[Link] EJS
[Link] EJS Templates
[Link] with Partials
12.1 Need of Dynamic UI
●Personalized Content: Tailors responses based on user
profiles, preferences, or behaviors to enhance user
experience.
●Dynamic Data Delivery: Provides real-time information
that updates with each request, such as live scores or
stock prices.
●Security and Access Control: Delivers different content
based on user authentication and authorization levels.
●Localization and Internationalization: Adjusts
responses to accommodate different languages, cultures,
or regional settings.
●API Versatility: Supports multiple client types (web,
mobile, IoT) by providing appropriate data formats and
structures.
12.2 Sharing using Global Variables
1.•CNlicke
•Scond level toe
Dtexyt
eddit
sntylaesmic
Moasfte r UI
•iFinfth gleve lusing Global Variables
•Third level
[Link]•Frourth level
[Link] is EJS
[Link] EJS
[Link] EJS Templates
[Link] with Partials
13. Model View Controller
[Link] of Concerns
[Link] First Controller
[Link] All Controllers
[Link] 404 Controller
[Link] Models
[Link] data to Files
[Link] causing problems
[Link] data from Files
13.1 Separation of Concerns
● MVC stands for Model-View-Controller: A
software architectural pattern for developing
user interfaces.
●Model: Manages the data and business logic
of the application.
●View: Handles the display and presentation
of data to the user.
●Controller: Processes user input, interacts
with the Model, and updates the View
accordingly.
●Routes are a part of Controllers.
●Purpose: MVC separates concerns within an
application, making it easier to manage and
scale.
13.2 Adding First Controller
13.3 Adding All Controllers
13.3 Adding All Controllers
13.3 Adding All Controllers
13.4 Adding 404 Controller
13.5 Adding Models
13.5 Adding Models
13.6 Writing data to Files
13.6 Writing data to Files
13.7 Nodemon causing problems
13.8 Reading data from Files
13.8 Reading data from Files
Practise Milestone
1.•SClicek
•Second level
pto aedrit aMatstieor tnex to stfyle sCo
•iFonurthg
[Link]•Tdhird level lev elFirst Controller
3. Adding All Controllers
•Fifth level
2.
3.
14.2 Adding Home Details Page
4.
5.
14.3 Showing Real Home Data
[Link] a static findById method in Home model that takes a callback.
[Link] this findById method in the controller to load home details and log
them.
[Link] change the controller:
a. Redirect to /homes in case the home is not found, logging an error.
b. Rendering the /home-detail page with home data.
4. Create a home-detail page to use the entire page to show all home data.
5. Download from Github:
a. The styled home-detail file.
b. 10 image of homes that you can put locally and use.
14.3 Showing Real Home Data
1.
2.
14.3 Showing Real Home Data
3.
14.3 Showing Real Home Data
4. 5.
14.4 Adding Favourites Feature
[Link] a partial with a form and a button that submits to /favourites path
2.
14.4 Adding Favourites Feature
3.
14.4 Adding Favourites Feature
4.
5.
14.5 Adding Favourites Model
1. Create a new Model to handle Favourites:
a. A static method getFavourites that reads the [Link] file and
b. return the ids of all homes marked favourite.
A static method addFavourites that adds the home-id to favourites-id
array if not already there, then updates the file.
2. Use this model in addFavourites controller.
3. Use this model in getFavourites controller while also fetching details of
4. all homes from Homes Model.
Change the UI of favourites page to show new content.
14.5 Adding Favourites Model
1.
14.5 Adding Favourites Model
2.
14.5 Adding Favourites Model
3.
14.5 Adding Favourites Model
4.
14.6 Edit-Home:
Adding Feature Skeleton
1.
2.
14.7 Edit-Home: Showing Existing Data
3.
14.6 Edit-Home: Adding Feature Skeleton
4.
5.
14.7 Edit-Home:
Showing Existing Data
[Link] the controller to now fetch the home details, if not found
redirect to /host/host-home-list otherwise passing the data to view.
[Link] the [Link] to show dynamic content based on values:
a. Different submit path.
b. Different button text.
c. Pre-filled values if present.
14.7 Edit-Home: Showing Existing Data
1.
14.7 Edit-Home: Showing Existing Data
2.
14.8 Edit-Home:
Handing Edit Request
1. Add a router for POST /edit-home
2. Add a controller for /edit-home, which creates a home model object
3. and saves it, before redirecting to /host/host-home-list
4. Add hidden id input field in the [Link] to get the id as part of
POST request.
Change the save method in Model to handle creation of new as well
as updation of existing Home.
14.8 Edit-Home: Handing Edit Request
1.
2.
3.
14.8 Edit-Home: Handing Edit Request
4.
14.9 Adding Delete Feature
[Link] the delete button with a form that submits to path
/host/delete-home/:homeId
[Link] a route in the host routes.
[Link] a static delete method to the Home model that takes an id and deletes
the home.
[Link] a method in host controller to handle the request, delete the home and
redirect to /host/host-homes-list page.
[Link]: deleted homes might still be in favourites list.
14.9 Adding Delete Feature
1.
2.
3.
14.9 Adding Delete Feature
4.
14.10 Removing Home from Favourites
[Link] a static method to the favourites model to delete the hom
[Link] a form to the favourites-list in each home to path
/favourites/delete/:homeId
[Link] a route for POST delete-favourites in the store routes.
[Link] a method in store controller to use the model’s static method and then
redirect to favourites-list.
[Link] this model method in home-delete to make sure any deleted home is
also removed from favourites.
14.10 Removing Home from Favourites
1.
2.
3.
14.10 Removing Home from Favourites
4.
5.
Revision
1.•WClichk atot eadirt eM adstyenr taemxt iscty lpeasths
2. A•dSedcond
•Tihnirdglevel
le vHelome Details Page
3. Show•iFnougrth
•FiftRh leeveall
level Home Data
4. Adding Favourites Feature
[Link] Favourites Model
6. Edit-Home: Adding Feature Skeleton
7. Edit-Home: Showing Existing Data
8. Edit-Home: Handing Edit Request
9. Adding Delete Feature
10. Removing Home from Favourites
15. Introduction to SQL
[Link] is a DB (Database)
[Link] to SQL DB
[Link] to NoSQL DB
[Link] vs NoSQL
[Link] MySQL
6. Connecting App to DB
7. Creating homes Table
[Link] homes in App
[Link] DB in Models
[Link] Home in Model
[Link] Model using Where
15.1 What is a DB (Database)
1. Store Data: Keep large amounts of data in a structured format.
2. Enable Data Management: Allow for adding, updating, and deleting
data easily.
3. Facilitate Quick Access: Provide fast retrieval of data through
queries.
4. Ensure Data Integrity: Maintain accuracy and consistency of data
over time.
5. Support Multiple Users: Handle concurrent access by many users
simultaneously.
6. Secure Data: Protect information through access controls and
authentication.
15.2 Introduction to SQL DB
● Vertical Scalability: Typically scaled
by increasing the resources of a single
server (scaling up).
● Relationships: Tables can have
multiple types of relationships.
6.
15.9 Adding DB in Models
7.
15.10 Adding Home in Model
[Link] the description field in home. Change constructor and usag
[Link] changes in UI to input and show it everywhere.
[Link] the save method using the insert query.
[Link] the usages of save method to use the promise.
15.10 Adding Home in Model
1.
15.10 Adding Home in Model
2.
15.10 Adding Home in Model
3.
4.
15.11 Implementing Model using Where
Practise Milestone
VS
16.2 Setting up MongoDB
16.2 Setting up MongoDB
16.2 Setting up MongoDB
16.2 Setting up MongoDB
16.3 Installing MongoDB Driver
16.3 Installing MongoDB Driver
16.3 Installing MongoDB Driver
16.3 Installing MongoDB Driver
16.4 Creating MongoDB Connection
16.5 Saving a Home
16.6 Install MongoDB Compass
16.6 Install MongoDB Compass
16.6 Install MongoDB Compass
16.6 Install MongoDB Compass
16.6 Install MongoDB Compass
16.6 Install MongoDB Compass
16.7 Install MongoDB for VSCode
16.7 Install MongoDB for VSCode
16.7 Install MongoDB for VSCode
16.7 Install MongoDB for VSCode
16.7 Install MongoDB for VSCode
16.8 Fetching all Homes
1. Basic Usage: [Link](query) retrieves
documents matching the query criteria.
2. Returns a Cursor: The method returns a
cursor, an iterator over the result set.
16.8 Fetching all Homes
16.9 Fetching one Home
16.9 Fetching one Home
16.9 Fetching one Home
16.9 Fetching one Home
16.10 Supporting Edit & Delete
16.11 Adding MongoDB to Favourite
[Link] all the file handling related code from Favourite Model.
[Link] the data folder.
[Link] the following methods in Favourite model to use mongo:
a. fetchAll
b. Change addToFavourites to save method
c. deleteById
[Link] the usages of Favourite model in StoreController to use the promise syntax
a. getFavourites
b. postAddFavourites
c. postRemoveFavourite
16.11 Adding MongoDB to Favourite
1. 2.
16.11 Adding MongoDB to Favourite
3.
16.11 Adding MongoDB to Favourite
4.a.
16.11 Adding MongoDB to Favourite
4.b.
16.11 Adding MongoDB to Favourite
4.c.
Practise Milestone
[Link] is Mongoose
[Link] up Mongoose
[Link] Home Schema
[Link] Home using Mongoose
5. Fetching Homes
[Link] one Home
[Link] a Home
[Link] a Home
[Link] Mongoose for Favourite
10. Fetching Relations
17.1 What is Mongoose
[Link] is an Object Data Modeling (ODM) library for MongoDB and [Link].
[Link] a schema-based solution to model application data.
[Link] data validation and type casting in [Link] applications.
[Link] easy interaction with MongoDB through intuitive methods.
[Link] middleware for pre and post-processing of data.
[Link] manage relationships between data with built-in functions.
17.2 Setting up Mongoose
17.2 Setting up Mongoose
[Link] Mongoose package.
[Link] and use mongoose in [Link]
[Link] the database-util file.
[Link] the usage of db-util from everywhere.
1.
17.2 Setting up Mongoose
2.
17.3 Create Home Schema
[Link] complete airbnb db from mongo.
[Link] the existing Home Model code.
[Link] the new Home Schema in the Home Model File.
17.4 Saving Home using Mongoose
17.5 Fetching Homes
17.6 Fetching one Home
1, 2.
17.9 Using Mongoose for Favourite
3.a.
17.9 Using Mongoose for Favourite
3.b.
17.9 Using Mongoose for Favourite
3.c.
17.9 Using Mongoose for Favourite
4.
17.10 Fetching Relations
Revision
1.•WClichk atot e disit Masotenr gteoxto sstyeles
2.S•eSetctonidn
•Third levelp
lgevMongoose
eul
[Link]•Feou rtHh
•Fifth levoel Schema
levmele
[Link] Home using Mongoose
5. Fetching Homes
[Link] one Home
[Link] a Home
[Link] a Home
[Link] Mongoose for Favourite
10. Fetching Relations
18. Cookies & Sessions
1.
18.2 Adding Login Functionality
2.
18.2 Adding Login Functionality
3.
18.2 Adding Login Functionality
3.
18.2 Adding Login Functionality
4, 5.
18.3 Checking Login State
[Link] a isLoggedIn field in the req object and use it everywhere
else.
[Link] a condition in the navigation ejs file that no path other
than home and login should be visible until a user has logged in.
[Link] all the render calls to send the flag
[Link] add a middleware for host routes that if the user is not
logged in they should be redirected to the login page.
18.3 Checking Login State
1.
18.3 Checking Login State
2.
18.3 Checking Login State
3.
18.3 Checking Login State
4.
18.4 Using a Cookie
[Link] why a global variable would not work.
[Link] a cookie on successful login. See it in storage, also on the
next request.
[Link] the cookie using syntax and Define a middleware to set
this value to the request object.
[Link]([Link]('Cookie').split('=')[1]);
[Link] the Cookie from the browser and see the result.
18.4 Using a Cookie
2.
3.
18.4 Using a Cookie
4.
18.5 Define the Logout Feature
[Link] a logout button in nav bar that should come only when
user is logged in. Button should be a form that submits to link
/logout.
[Link] the login button in case user is logged in.
[Link] the logout path and set the isLoggedIn cookie to false.
18.5 Define the Logout Feature
1, 2.
18.5 Define the Logout Feature
3.
18.6 Problem with Cookies
[Link] can be intercepted or stolen, posing
security risks.
[Link] have limited storage capacity (about
4KB).
[Link] can delete or modify cookies, leading
to data loss or tampering.
[Link] in cookies is not encrypted, making
sensitive information vulnerable.
[Link] important info in cookies exposes it
to client-side attacks.
18.7 What are Sessions
3.
4.
18.9 Creating a Sessions
5.
18.10 Saving Session in DB
18.10 Saving Session in DB
Practise Milestone
1.
19.5 Signup UI
2.
19.5 Signup UI
3, 4.
19.6 Using Express Validator
[Link] handling for POST /signup in auth controller and router.
[Link] Express Validator.
[Link] the email and password validations in the post handler.
[Link] the [Link] to show the errors. And accept the old
values.
19.6 Using Express Validator
1.
2.
19.6 Using Express Validator
3.
19.6 Using Express Validator
3.
19.6 Using Express Validator
4.
19.7 Adding User Model
[Link] a new User Model with following fields:
[Link] & lastName (required)
[Link] (required, unique)
[Link] (required)
[Link] (possible values ‘guest’, ‘host’)
[Link] the POST signup handler, create a new user with the fields
from request and redirect to login after saving the user.
19.7 Adding User Model
1.
19.7 Adding User Model
2.
19.8 Encrypting Password
[Link] the problem with plain text passwords.
[Link] a package named: bcryptjs
[Link] the password before saving password.
[Link] that even server does not have the password.
19.8 Encrypting Password
2.
19.8 Encrypting Password
3.
19.9 Implementing Login
[Link] the email and password from the request body and
find the user with the email from the Users collection.
[Link] the user is not found send an error and re-render the
login page, also make changes to the login page to show
errors.
[Link] the user is found, then use the bcrypt compare function
to match the entered password, If password does not
match send another error, otherwise create a login session
and redirect user to the home.
19.9 Implementing Login
1.
19.9 Implementing Login
2.
19.9 Implementing Login
3.
19.10 Adding User Functions
[Link] the navigation bar items display only on the basis of userType by
passing the user object to all views.
[Link] a field in User Model names favouriteHomes, which is an array of
home ids.
[Link] the Favourite Model and the Pre hook from the Home Model.
[Link] the favourite user specific and change the following methods:
a. postAddFavourites
b. getFavourites
c. postRemoveFavourite
19.10 Adding User Functions
1.
19.10 Adding User Functions
2.
19.10 Adding User Functions
4.a.
19.10 Adding User Functions
4.b.
19.10 Adding User Functions
4.c.
Revision
1.•WClichk atot e diist MAasutetr htextn styilcesation
•aThtird
2.W•Shecond liesve lAuthorization
level
•nFi ftbh
[Link]•ioFourth levelleavesled Authentication
[Link] vs Authorization
[Link] UI
[Link] Express Validator
[Link] User Model
8. Encrypting Password
[Link] Login
[Link] User Functions
20. File Upload & Download
[Link] Files
20.1 Adding a File Picker
Adding this filter, now multer would not have the file
if it does not match the type and [Link] will just be
undefined.
20.6 Restricting Upload File Types
20.7 Handling Edits
While editing we can make sure that we only overwrite the photoUrl in
case a new valid image was uploaded. And don’t force the user to upload
images each time they edit.
20.8 Serving Saved Data
●REST APIs enable communication between clients and servers using HTTP.
●They are mainly identified by a URI.
●They use standard HTTP methods like GET, POST, PUT, and DELETE.
●Data is exchanged in formats like JSON or XML.
● REST APIs are stateless.
●REST APIs allow clients to access and manipulate web resources.
21.3 Decoupling Frontend & Backend
●Statelessness: Each request contains all necessary information; the server maintains no
client session.
●Uniform Interface: Standardized communication using HTTP methods like GET, POST, PUT,
DELETE.
●Client-Server Separation: Independent development of front-end and back-end components.
●Cacheability: Responses indicate if they can be cached to improve performance.
●Layered System: Architecture allows for multiple layers between client and server.
●Code on Demand (Optional): Servers can extend client functionality by sending executable
code.
21.6 First API Todo App
[Link] the following from the previous app:
a. Env files
b. Package dependencies
c. [Link]
2. Changes in env files
a. Remove the email data.
b. Change DB name to todo-app
[Link] empty controllers, models, and routes folders.
[Link] excess code from [Link].
[Link] the error controller to give out a 404 error message and status.
[Link] a Mongoose model for TodoItem.
[Link] itemsRouter and itemsController for POST /todos request from frontend.
[Link] and setup CORS package in backend.
9. Add [Link]() to app.
21.6 First API Todo App
1.
3.
2.
21.6 First API Todo App
4.
21.6 First API Todo App
6.
5.
21.6 First API Todo App
7.
21.6 First API Todo App
8,9.
21.7 API for Fetch Items
21.8 API for Deleting Items
21.9 Improving UI Elements
[Link] bootstrap
[Link] tailwind to the project.
[Link] the whole app using tailwind classes.
21.10 Adding Complete Item functionality
4.
Revision
1.•WClichk taot e daitr Meas tAer steyxnt scty lResequests
•aThitrd
2.W•Shecond leaverle REST APIs
level
•Flifithnlevel
[Link]•uFouprth legvel Frontend & Backend
[Link] & HTTP Methods
[Link] Core Concepts
[Link] API Todo App
[Link] for Fetch Items
[Link] for Deleting Items
[Link] UI Elements
[Link] Complete Item functionality