Handling Requests and Responses in Express.js
Handling Requests and Responses in Express.js
Handling requests and responses in Express.js is a fundamental concept in backend development. Every time a user interacts with an application, a request is sent to the server, and the server sends back a response. In Express.js, handling requests and responses is simple and efficient using built-in methods.
What is Request in Express.js
A request in Express.js is an object that contains information sent by the client. It includes data such as URL, parameters, query strings, headers, and body data.
Example of Request Object
app.get(‘/user/:id’, (req, res) => {
console.log(req.params.id);
res.send(‘User Data’);
});
In this example, the request object is used to get route parameters.
What is Response in Express.js
A response in Express.js is an object used to send data back to the client. You can send text, JSON, HTML, or status codes using the response object.
Example of Response Object
app.get(‘/’, (req, res) => {
res.send(‘Hello World’);
});
Handling Route Parameters in Express.js
Example of Route Parameters
app.get(‘/product/:id’, (req, res) => {
res.send(Product ID: ${req.params.id});
});
Route parameters are used to handle dynamic values in URLs.
Handling Query Parameters in Express.js
Example of Query Parameters
app.get(‘/search’, (req, res) => {
res.send(Search: ${req.query.q});
});
Query parameters are used to send additional data in the URL.
Handling Request Body in Express.js
Enable JSON Middleware
app.use(express.json());
Example of Request Body
app.post(‘/login’, (req, res) => {
const { username, password } = req.body;
res.send(User: ${username});
});
The request body is used to send data from the client to the server.
Sending Different Types of Responses in Express.js
Send Text Response
res.send(‘Hello’);
Send JSON Response
res.json({ message: ‘Success’ });
Send Status Code
res.status(200).send(‘OK’);
These methods help send different types of responses in backend applications.
Redirect and Send File in Express.js
Redirect Example
res.redirect(‘/home’);
Send File Example
res.sendFile(__dirname + ‘/index.html’);
These methods are used in real-world applications for navigation and file handling.
Real-World Use of Request and Response
Handling requests and responses in Express.js is used in login systems, APIs, form submissions, and data processing. It is a core part of backend development.
Best Practices for Handling Requests and Responses
Validate Input
Always validate user input before processing requests.
Use Proper Status Codes
Send correct HTTP status codes for better API design.
Keep Responses Clean
Send structured and meaningful responses.
FAQs on Requests and Responses in Express.js
What is request object in Express.js
The request object contains data sent by the client.
What is response object in Express.js
The response object is used to send data back to the client.
How to handle JSON data in Express.js
Use express.json() middleware to handle JSON data.
Continue Learning Backend Development
Now that you understand handling requests and responses in Express.js, you are ready to move to the next lesson where you will learn about building REST APIs in Node.js. To explore more structured tutorials and courses, click here for more free courses.



