The HTTP request-response cycle is how a client communicates with a server. In Express.js, the server receives, processes, and responds to each client request.
- 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
Properties | Description |
|---|---|
It is useful when you need to access application-level properties or methods within a middleware function or route handler. | |
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. | |
It contains cookies sent by the client in the request and is used with the cookie-parser middleware. | |
It is the remote IP address of the request. | |
It contains the path part of the request url. | |
It contains the currently matched route. | |
It is an object containing properties mapped to the named route โparametersโ | |
It allows you to access the query parameters from the URL of an incoming HTTP request. | |
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. |
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
Properties | Description |
|---|---|
It holds a reference to the instance of the Express app that is using the middleware. | |
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 | |
It is used to set a cookie with the specified name and value. | |
It returns the current value of the specified response header(header). | |
It ends the current response process. | |
It is used to send a JSON response to a client. | |
It allows you to include link headers in your HTTP responses. | |
It is used to render a view template & send the resulting HTML to the client. | |
It is used to set the Location HTTP response header to the specified path or URL. | |
It is used to send a response to the client. | |
It is used to set the response HTTP header field to value. | |
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
In Express.js, HTTP status codes are 3-digit codes that indicate the result of a client request, such as success, failure, or further action. Using appropriate status codes improves API communication and debugging.
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'); |