0% found this document useful (0 votes)
11 views6 pages

BEE 4th Semester

The document provides an overview of Client-Server Architecture, detailing its components, request handling process, and types of architectures. It also introduces Node.js as a server-side runtime environment, highlighting various frameworks for web development, particularly Express.js. Key features of Express.js, including routing, middleware, and request handling, are explained to illustrate its utility in building web applications and APIs.

Uploaded by

anishka0112.be23
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)
11 views6 pages

BEE 4th Semester

The document provides an overview of Client-Server Architecture, detailing its components, request handling process, and types of architectures. It also introduces Node.js as a server-side runtime environment, highlighting various frameworks for web development, particularly Express.js. Key features of Express.js, including routing, middleware, and request handling, are explained to illustrate its utility in building web applications and APIs.

Uploaded by

anishka0112.be23
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/ 6

Introduction to Client-Server Architecture

1. Overview of Client-Server Architecture


Client-Server Architecture is a fundamental computing model used in networking, web
applications, and distributed systems. It consists of two main entities:

●​ 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.

2. Key Components of Client-Server Architecture


A. Client

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.

C. Network (Communication Medium)


●​ Acts as a bridge between the client and server, typically using the Internet or
Intranet.
●​ Communication happens using standard protocols such as HTTP, HTTPS, TCP/IP,
FTP, WebSocket, etc.

3. How Requests are Handled at the Server?


Step 1: Client Sends a Request

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 is formatted as an HTTP request (if it is a web-based request).


●​ The request includes essential details such as the HTTP method (GET, POST, PUT,
DELETE), headers, parameters, and body content.

Step 2: Request Reaches 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).

Step 3: Server Processes the Request

The server follows these steps:

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.

Step 4: Server Sends a Response

●​ 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

●​ The client interprets the response and displays the result.


●​ If the response is a web page, the browser renders the HTML, CSS, and
JavaScript.
●​ If it's an API response, the client processes the JSON/XML data accordingly.

4. Types of Client-Server Architectures


A. Two-Tier Architecture

●​ 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

●​ Client → Application Server → Database Server


●​ The application server acts as a mediator, processing business logic.
●​ Example: Web applications using Node.js as an application server and MySQL as
a database server.

C. Multi-Tier (N-Tier) Architecture

●​ Client → Load Balancer → API Gateway → Application Servers → Database


Servers
●​ Used in cloud computing, microservices, and enterprise applications.
●​ Provides better scalability and fault tolerance.

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.

●​ Middleware in Express.js provides a powerful way to handle requests and


responses and manage different aspects of request processing.
●​ Routing is straightforward with Express, allowing you to define routes and handle
HTTP requests effectively.
●​ Static file serving is handled easily with the express.static middleware.
●​ Error handling is crucial in Express applications to manage unexpected issues
and provide feedback to users.

Routing in Express.js

1. Routing Methods:

●​ app.get(path, callback): Handles GET requests.


●​ app.post(path, callback): Handles POST requests.
●​ app.put(path, callback): Handles PUT requests.
●​ app.delete(path, callback): Handles DELETE requests.
●​ app.patch(path, callback): Handles PATCH requests.

2. Route Paths:

●​ Static Paths: Simple paths like /home or /about.


●​ Dynamic Paths: Include route parameters, e.g., /user/:id where :id is a
placeholder for dynamic values.

3. Route Parameters:

●​ Extracted from the URL, e.g., req.params.id in the route /user/:id.


●​ Useful for accessing data associated with a particular resource.

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

●​ res.send(body): Sends the HTTP response.


●​ res.json(json): Sends a JSON response.
●​ res.redirect([status], path): Redirects to a different URL.
●​ res.render(view, [locals]): Renders a view template and sends the HTML
response.
●​ res.status(code): Sets the HTTP status code.

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:

Defined at the application level using app.use():​



app.use((req, res, next) => {
console.log('Request received');
next(); // Passes control to the next middleware
});
3. Router-Level Middleware:

Used with instances of express.Router():​


const router = express.Router();
router.use((req, res, next) => {
console.log('Router-level middleware');
next();
});

4. Error-Handling Middleware:

Defined with four arguments: err, req, res, next:​


app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something broke!');
});

5. Third-Party Middleware:

Middleware provided by third-party libraries, such as body-parser, cors, etc.​


const bodyParser = require('body-parser');
app.use(bodyParser.json());

Request Handling in Express.js

●​ 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.

You might also like