The HTTP request and response cycle is the process through which a client communicates with a server. In Express.js, every client request is received by the server, processed, and a response is sent back to the client. Understanding this cycle is essential for building web applications and APIs.
- It defines how a client and server communicate over HTTP.
- Every request is processed before a response is returned.
- Express.js uses the req and res objects to handle this communication.

Request Object
The Request Object (req) represents the HTTP request sent by the client to the Express server. It contains information about the request, such as the URL, route parameters, query parameters, request body, headers, and cookies.
Syntax:
app.get('/', (req, res) => {
// Access request data using req
});
Request Object Properties
S.NO | Properties | Description |
|---|---|---|
1 | It is useful when you need to access application-level properties or methods within a middleware function or route handler. | |
2 | It is primarily used to access data submitted by a client (e.g., web browser, mobile app) to the server, typically through HTTP method like POST, PUT , or PATCH. | |
3 | It contains cookies sent by the client in the request and is used with the cookie-parser middleware. | |
4 | It is the remote IP address of the request. | |
5 | It contains the path part of the request url. | |
6 | It contains the currently matched route. | |
7 | It is an object containing properties mapped to the named route โparametersโ | |
8 | It allows you to access the query parameters from the URL of an incoming HTTP request. | |
9 | req.files | It is an object that contains uploaded files sent through an HTTP request using multipart/form-data encoding when using file upload middleware. |
10 | It returns the matching content-type if the incoming request's 'content-type' HTTP header field matches with the MIME type that has been specified by the type parameter & it returns null if the request has no body otherwise it returns false. |
Response Object
The Response Object (res) is passed as the second parameter to the route handler. It is used to send responses such as HTML pages, JSON data, files, images, or status codes back to the client.
Response Object Properties
S.NO | Properties | Description |
|---|---|---|
1 | It holds a reference to the instance of the Express app that is using the middleware. | |
2 | It appends the specified value to the HTTP response header field & if the header is not already set then it creates the header with the specified value | |
3 | It is used to set a cookie with the specified name and value. | |
4 | It returns the current value of the specified response header(header). | |
5 | It ends the current response process. | |
6 | It is used to send a JSON response to a client. | |
7 | It allows you to include link headers in your HTTP responses. | |
8 | It is used to render a view template & send the resulting HTML to the client. | |
9 | It is used to set the Location HTTP response header to the specified path or URL. | |
10 | It is used to send a response to the client. | |
11 | It is used to set the response HTTP header field to value. | |
12 | It is used to set the HTTP status code for a response. |
Methods to Send Request to Server
1. Client Sends a Request
The cycle starts when a clients - such as browser , mobile app or API testing tool(like postman)- sends an HTTP request to the server.
This request includes:
- HTTP method (e.g., GET, PUT, POST, DELETE)
- URL/EndPoint (e.g., /users, /products/1)
- Headers (e.g., content-type, authorization)
- Optional Data(like form-data, or JSON in the request body)
2. Express Receives the Request
Express.js listens for incoming requests on the specified routes and HTTP methods. When a matching route is found, it passes the request to the corresponding route handler.
filename: app.js
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('App created successfully');
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
Console Output:

Browser Output:

3. Middleware Processing
Before reaching the router handler, the request can pass through one or more middleware functions. Middleware can modify the req (request) or perform actions like authentication , logging, or parsing data.
filename: app.js
app.use((req, res, next) => {
console.log("Request received");
next();
});
4. Route Handler Executes
The matched route executes its callback function, where you can access request data using the req object and send a response using the res object.
filename: express.js
const express = require('express');
const app= express();
app.get('/user',(req,res)=>{
res.send('Data added Successfully!')
}).listen(8080,()=>{
console.log('User Data Saved!')
})
To run the file use node <filename>
Output:
Note: the output will run on localhost:8080/user, where /user will the user endpoint
5. Server Sends a Response
Using the res object , Express sends the response back to the client. You can send plain text, JSON , HTML or status code.
filename: app.js
app.get('/success', (req, res) => {
res.status(200).json({ message: "Success" });
});
6. Cycle Completes
Once the response is sent , the cycle ends. The client receives the result, and may act on it or display it to the user.
Status Code
When a server responds to client request in Express.js , it returns more than just a data - it also checks an HTTP Status code. These codes are 3- digit numbers that indicates the result of the request. it helps the client to understand whether the request is successful, failed or requires further action. To use appropriate status code improves communication between client and server, enhances debugging and ensures better API design.
Common HTTP Status Code and their Usage in Express.js
Status Code | Meaning | Example |
|---|---|---|
200 OK | Success | res.status(200).send('Success') |
201 Created | Resource Created | res.status(201).json({message: 'User Created'}); |
204 No Content | Success with no Response Body | res.status(204).send(); |
400 Bad Request | Client Error | res.status(400).send('Client Error'); |
401 Unauthorized | Authentication Required | res.status(401).json({error: ''Unauthorized'}); |
403 Forbidden | Access Denied | res.status(403). send('Access Denied'); |
404 Not Found | Resource Not Found | res.status(404). send('Resource Not Found'); |
500 Internal Server Error | Internal Server Error | res.status(500). send(' Internal Server Error'); |