request and response
1. What Request and Response are
In Express.js:
req(request) is the object representing the incoming HTTP request from the client;res(response) is the object the server uses to send a response back to the client.
When a client makes a request (say, GET /users?id=10), Express automatically creates these two objects and passes them into your route handler.
Example:
const express = require('express');
const app = express();
app.get('/hello', (req, res) => {
console.log(req.method); // "GET"
console.log(req.url); // "/hello"
res.send('Hello, Express!');
});
app.listen(3000);Client → server:
GET /helloThe server responds:
Hello, Express!2. The req (Request) object
reqis the object representing the HTTP request that came in from the client.
It holds everything the client sent: the URL, parameters, body, headers, and so on.
req's main properties
| Property | Description | Example |
|---|---|---|
req.method | The request's HTTP method | 'GET', 'POST', 'PUT', 'DELETE' |
req.url | The full request URL | '/users?name=tim' |
req.path | Just the path, no query | '/users' |
req.query | The query-parameters object | { name: 'tim' } |
req.params | The route parameters | /users/:id → { id: '123' } |
req.body | The request body (for POST/PUT) | { name: 'John' } (with express.json()) |
req.headers | The HTTP headers | { 'content-type': 'application/json' } |
req.cookies | Cookies (with cookie-parser attached) | { token: 'abc123' } |
req.ip | The client's IP address | '127.0.0.1' |
req.protocol | 'http' or 'https' | 'http' |
req.get(headerName) | Get a specific header | req.get('User-Agent') |
An example request with parameters:
app.get('/user/:id', (req, res) => {
console.log(req.params.id); // path: /user/42 → "42"
console.log(req.query); // query: ?name=Tim → { name: 'Tim' }
res.send('User found');
});3. The res (Response) object
resis the object Express uses to send a response back to the client.
You can:
- return text, JSON, HTML;
- set the status, headers, cookies;
- end the response.
res's main methods
| Method | Purpose | Example |
|---|---|---|
res.send(data) | Send text, HTML, or JSON | res.send('Hello!') |
res.json(obj) | Send a JSON object | res.json({ ok: true }) |
res.status(code) | Set the response status | res.status(404).send('Not Found') |
res.set(header, value) | Set an HTTP header | res.set('X-Powered-By', 'Express') |
res.redirect(url) | Redirect the client | res.redirect('/login') |
res.download(file) | Send a file for download | res.download('report.pdf') |
res.sendFile(path) | Send a file as the response | res.sendFile(__dirname + '/index.html') |
res.end() | End the response with no body | res.end() |
An example with JSON and a status
app.post('/api/users', (req, res) => {
const user = req.body;
console.log(user);
res.status(201).json({
message: 'User created',
data: user
});
});Response:
{
"message": "User created",
"data": { "name": "Tim", "age": 25 }
}4. How Express ties req and res together
Every time a client makes an HTTP request:
Client → [Express App] → Middleware → Route Handler → res.send()Express creates a pair of objects:
req, to carry the request's data;res, to send back the response.
They only live for the duration of handling one request.
5. An example using both objects
app.post('/feedback', (req, res) => {
const { name, message } = req.body;
console.log(`From ${name}: ${message}`);
res.status(200).send(`Thank you, ${name}, your message has been received!`);
});6. Summary
req(Request) is the incoming-request object: it holds the method, path, parameters, body, headers, and other client data.
res(Response) is the response object: used to send data to the client and set the status and headers.Together they form the foundation of the client-server interaction in Express.js.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.