Loading

Quipoin Menu

Learn • Practice • Grow

express-js / ExpressJS Request & Response Object
tutorial

ExpressJS Request & Response Object

Imagine you are at a restaurant. When you place an order (request), the waiter takes your order and brings it to the kitchen. After the food is prepared, the waiter brings it back to you (response). In Express, the request and response objects work exactly like this waiter they carry information from the client to the server and back.

The Request Object (req)

The request object contains information about the HTTP request that came from the client. It's usually abbreviated as req in route handlers. Think of it as a package containing everything the client sent.

Commonly Used Request Properties

  • req.params Route parameters (like /users/:id)
  • req.query Query string parameters (like ?name=John)
  • req.body Data sent in the request body (POST/PUT requests)
  • req.headers HTTP headers sent by the client
  • req.method HTTP method (GET, POST, etc.)
  • req.url The full URL path
  • req.path The path part of the URL
  • req.cookies Cookies sent by the client (requires cookie-parser)

Example: Accessing Request Properties
app.get('/users/:userId', (req, res) => {
// Route parameters: /users/123 -> { userId: '123' }
console.log(req.params);

// Query parameters: /users/123?sort=asc&page=2
console.log(req.query); // { sort: 'asc', page: '2' }

// HTTP method
console.log(req.method); // 'GET'

// Headers
console.log(req.headers['user-agent']);

// Full URL
console.log(req.url); // '/users/123?sort=asc&page=2'

res.send('User page');
});

The Response Object (res)

The response object is used to send data back to the client. It contains methods for sending different types of responses. Think of it as your way to communicate back to the browser or client.

Commonly Used Response Methods

  • res.send() Send a response (string, HTML, JSON, buffer)
  • res.json() Send JSON response
  • res.status() Set HTTP status code
  • res.redirect() Redirect to another URL
  • res.render() Render a view template
  • res.sendFile() Send a file
  • res.cookie() Set a cookie
  • res.clearCookie() Clear a cookie
  • res.set() Set response headers

Example: Using Response Methods
app.get('/api/user', (req, res) => {
// Send JSON response
res.json({
name: 'John Doe',
email: 'john@example.com'
});
});

app.get('/redirect', (req, res) => {
// Redirect to another page
res.redirect('/home');
});

app.get('/custom-status', (req, res) => {
// Send with custom status code
res.status(201).send('Created successfully');
});

app.get('/set-header', (req, res) => {
// Set custom headers
res.set('X-Custom-Header', 'Hello');
res.send('Header set');
});

Chaining Response Methods

Most response methods are chainable, meaning you can call multiple methods in sequence:
app.get('/user/:id', (req, res) => {
res
.status(200)
.set('Content-Type', 'application/json')
.json({ id: req.params.id, name: 'John' });
});

Two Minute Drill
  • req contains information about the incoming request (params, query, body, headers).
  • res is used to send responses back to the client.
  • Use req.params for route parameters, req.query for query strings, and req.body for POST data.
  • Use res.json() for APIs, res.send() for simple responses, and res.status() to set HTTP status codes.
  • Response methods are chainable for cleaner code.

Need more clarification?

Drop us an email at career@quipoinfotech.com