Skip to main content

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:

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

PropertyDescriptionExample
req.methodThe request's HTTP method'GET', 'POST', 'PUT', 'DELETE'
req.urlThe full request URL'/users?name=tim'
req.pathJust the path, no query'/users'
req.queryThe query-parameters object{ name: 'tim' }
req.paramsThe route parameters/users/:id{ id: '123' }
req.bodyThe request body (for POST/PUT){ name: 'John' } (with express.json())
req.headersThe HTTP headers{ 'content-type': 'application/json' }
req.cookiesCookies (with cookie-parser attached){ token: 'abc123' }
req.ipThe client's IP address'127.0.0.1'
req.protocol'http' or 'https''http'
req.get(headerName)Get a specific headerreq.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

MethodPurposeExample
res.send(data)Send text, HTML, or JSONres.send('Hello!')
res.json(obj)Send a JSON objectres.json({ ok: true })
res.status(code)Set the response statusres.status(404).send('Not Found')
res.set(header, value)Set an HTTP headerres.set('X-Powered-By', 'Express')
res.redirect(url)Redirect the clientres.redirect('/login')
res.download(file)Send a file for downloadres.download('report.pdf')
res.sendFile(path)Send a file as the responseres.sendFile(__dirname + '/index.html')
res.end()End the response with no bodyres.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]MiddlewareRoute 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.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.