BEE 4th Semester
BEE 4th Semester
● Client: The device or software that initiates requests for services or resources.
● Server: The powerful machine or software that processes client requests and
returns responses.
This architecture enables efficient resource sharing, centralized control, and scalability
across various applications, including web applications, database systems, and cloud
computing.
The client is a device or application that sends a request to the server. Clients can be:
1. Thin Clients: Minimal processing power, relying on the server for operations (e.g.,
Web Browsers).
2. Thick Clients (Fat Clients): Perform some processing locally before requesting
data from the server (e.g., Desktop Applications).
B. Server
A server is a dedicated system that manages client requests, processes them, and sends
back responses. Common types of servers include:
● Web Server (e.g., Apache, Nginx, IIS) – Handles HTTP requests and serves web
pages.
● Application Server (e.g., Node.js, Tomcat, Django, Flask) – Manages application
logic and backend processing.
● Database Server (e.g., MySQL, PostgreSQL, MongoDB) – Manages and processes
database queries.
When a user interacts with a client application (e.g., entering a URL in a browser), a
request is sent to the server.
● The request travels through the network via routers, firewalls, and load balancers
before reaching the server.
● The server listens for incoming requests using ports (e.g., Port 80 for HTTP, Port
443 for HTTPS).
1. Authentication & Authorization – Verifies the client’s identity (e.g., using OAuth,
JWT, or API keys).
2. Routing the Request – Determines which resource or API endpoint should handle
the request.
3. Processing Business Logic – Executes backend code to fetch data, update
records, or perform calculations.
4. Database Interaction – If needed, the server queries the database for the
requested data.
● The server processes the request and sends back an HTTP response.
● A response includes:
○ Status Code (e.g., 200 OK, 404 Not Found, 500 Internal Server Error).
○ Headers (e.g., Content-Type, Cache-Control).
○ Body (e.g., JSON data, HTML page, error message).
Step 5: Client Receives and Renders the Response
● Client → Server
● Direct communication between the client and server.
● Example: A simple web application where the client sends requests, and the
server directly fetches data from a database.
B. Three-Tier Architecture
Introduction to Node.js
Node.js is a powerful runtime environment that allows you to run JavaScript on the
server side. It uses the V8 JavaScript engine developed by Google and is designed for
building scalable network applications.
Node.js has several frameworks that can simplify web development by providing various
utilities and abstractions:
1. Express.js: The most popular and minimalistic framework, designed to build web
applications and APIs quickly.
2. Koa.js: Created by the same team that made Express, Koa is more modern and
provides a lighter, more modular approach to middleware.
3. Hapi.js: Known for its rich plugin system and fine-grained control over request
handling, useful for building large-scale applications.
4. NestJS: A framework that uses TypeScript by default and provides a more
structured and scalable approach to building server-side applications, inspired by
Angular.
5. Sails.js: A MVC framework for building data-driven APIs, built on top of Express
and designed to facilitate the development of real-time applications.
6. LoopBack: A highly extensible framework for creating APIs and connecting to
data sources.
Express.js
Express.js is a widely used web application framework for Node.js. It simplifies the
process of building web applications and APIs by providing a robust set of features and
utilities.
Routing in Express.js
1. Routing Methods:
2. Route Paths:
3. Route Parameters:
4. Route Handlers:
● Functions that handle requests to a route. You can define multiple handlers for
different HTTP methods or different routes.
Example:
app.get('/user/:id', (req, res) => {
const userId = req.params.id;
// Handle request
});
Response Methods
Middleware
1. Middleware Lifecycle:
● Middleware functions are executed sequentially for each request. They can modify
the request object, the response object, or end the request-response cycle.
2. Application-Level Middleware:
4. Error-Handling Middleware:
5. Third-Party Middleware:
● Request Flow:
○ Request enters Express.
○ It passes through middleware functions (app-level, router-level).
○ It hits the route handler.
○ The response is sent back through the middleware (error-handling if
needed).
● Blocking vs Non-Blocking Code:
○ Blocking Code: Executes sequentially, each operation must complete
before moving on. Can lead to performance issues in Node.js.
○ Non-Blocking Code: Asynchronous, allows Node.js to handle multiple
operations concurrently. Utilizes callbacks, promises, or async/await.