Suggest an editImprove this articleRefine the answer for “request and response”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)`req` (request) represents the client's incoming HTTP request (method, URL, query, params, body, headers), while `res` (response) is the object the server uses to send the response back (text, JSON, status, headers, redirects); Express creates this pair for every request and passes it into the route handler. **Key point:** both objects only live for the duration of handling one specific request - `req.params` gives you the path parameters (`/users/:id`), `req.query` the query string, and `req.body` the body (which needs `express.json()`).Shown above the full answer for quick recall.Answer (EN)Image## 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: ```javascript 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: ```javascript GET /hello ``` The server responds: ```javascript Hello, Express! ``` ## 2. The `req` (Request) object > `req` is 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: ```javascript 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 > `res` is 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 ```javascript app.post('/api/users', (req, res) => { const user = req.body; console.log(user); res.status(201).json({ message: 'User created', data: user }); }); ``` Response: ```javascript { "message": "User created", "data": { "name": "Tim", "age": 25 } } ``` ## 4. How Express ties req and res together Every time a client makes an HTTP request: ```javascript 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 ```javascript 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.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.