Here’s a breakdown of commonly used parameters like req.body, req.query, req.params, and req.headers, including what they represent, common use cases, and code examples.

1. req.body

  • Definition:

    • req.body contains data sent by the client in the body of a POST, PUT, or PATCH request. This data can be sent in various formats, such as JSON or URL-encoded form data. Express uses middleware like express.json() or express.urlencoded() to parse the incoming request body.
  • Common Use Cases:

    • When clients submit form data, or when they send JSON payloads in POST requests (such as for creating or updating resources).
  • Example Usage:

// Middleware to parse JSON data
app.use(express.json());
 
app.post('/create-user', (req, res) => {
  const { username, email } = req.body;  // Accessing data from the body
  res.json({ message: `User ${username} created with email ${email}` });
});
 
// Client-side example using Axios
axios.post('/create-user', { username: 'john', email: 'john@example.com' });
  • Formats of req.body:
    • JSON: { "username": "john", "email": "john@example.com" }
    • URL-encoded form: username=john&email=john@example.com

2. req.query

  • Definition:

    • req.query contains the query string parameters found in the URL of a GET request. Query strings are appended to the end of a URL, following a ? symbol, and are useful for passing non-sensitive data that doesn’t affect the route structure.
  • Common Use Cases:

    • Filtering, sorting, and pagination, or specifying additional parameters without affecting the resource path.
  • Example Usage:

app.get('/search', (req, res) => {
  const { keyword, limit } = req.query;  // Accessing query string parameters
  res.json({ message: `Search for ${keyword} with limit of ${limit}` });
});
 
// URL: /search?keyword=nodejs&limit=10
// Client-side example:
axios.get('/search', { params: { keyword: 'nodejs', limit: 10 } });
  • Formats of req.query:
    • ?keyword=nodejs&limit=10
    • The portion before the ? is the base url, and multiple parameters are separated by &
    • Use arrays and nested objects: ?tags=tech&tags=js&filters[year]=2023

Use of colon ( :)

In Express.js, route parameters (or params) in the URL path must always begin with a colon (:). This colon indicates that the part of the URL is a dynamic value, and it is expected to change based on the incoming request. Without it, the URL segment is static.

 

app.get(‘/user/:id’, (req, res) { const userId = req.params.id; // Accessing the dynamic part of the URL res.send(User ID is ${userId}); });

  • /user/:id: The :id is the route parameter.
  • If a request is made to /user/42, req.params.id will hold the value 42.

Multiple Parameters:

We can have multiple dynamic segments in a route as well:

app.get('/product/:category/:id', (req, res) => {
  const { category, id } = req.params;
  res.send(`Category: ${category}, Product ID: ${id}`);
});
  • Request to /product/electronics/55 would result in category = 'electronics' and id = '55'.

3. req.params

  • Definition:

    • req.params contains route parameters, which are variables specified in the URL path. These are typically used when the path itself is dynamic, and specific segments of the URL represent certain resource identifiers.
  • Common Use Cases:

    • Handling dynamic resources such as user IDs, product IDs, or any resource that has a unique identifier.
  • Example Usage:

app.get('/user/:id', (req, res) => {
  const { id } = req.params;  // Accessing route parameters
  res.json({ message: `Fetching user with ID: ${id}` });
});
 
// URL: /user/123
// Client-side example:
axios.get('/user/123');
  • Formats of req.params:
    • Defined in route path as :parameter: /user/:id
    • The URL /user/123 will match /user/:id, and req.params.id will be 123.

4. req.headers

  • Definition:

    • req.headers contains the HTTP headers sent by the client. Headers can contain metadata, authentication tokens, content type, or user-agent information. Headers are key-value pairs sent with HTTP requests.
  • Common Use Cases:

    • Passing authorization tokens, content type, API keys, or any information related to the client’s environment.
  • Example Usage:

app.get('/protected', (req, res) => {
  const authHeader = req.headers['authorization'];  // Accessing headers
  if (authHeader) {
    res.json({ message: 'Authenticated' });
  } else {
    res.status(401).json({ message: 'No Authorization header provided' });
  }
});
 
// Client-side example with Axios:
axios.get('/protected', {
  headers: { Authorization: 'Bearer your_token_here' },
});
  • Common Headers:
    • Authorization: Used for passing tokens (e.g., Bearer tokens).
    • Content-Type: Specifies the media type of the resource, often used with application/json or application/x-www-form-urlencoded.
    • User-Agent: Information about the client application (e.g., browser).

Other Common Parameters/Properties:

  • req.method:

    • The HTTP method used for the request (e.g., GET, POST, PUT, DELETE).
    app.use((req, res, next) => {
      console.log(req.method);  // Logs the request method
      next();
    });
  • req.url:

    • The full URL that was requested.
    app.get('*', (req, res) => {
      console.log(req.url);  // Logs the requested URL
      res.send('URL logged');
    });
  • req.cookies:

    • If using cookie-parser middleware, req.cookies contains the cookies sent by the client.
    const cookieParser = require('cookie-parser');
    app.use(cookieParser());
     
    app.get('/show-cookies', (req, res) => {
      console.log(req.cookies);  // Logs the cookies sent by the client
      res.send('Cookies logged');
    });

Common Use Cases for Combining Parameters:

  1. Handling a POST request with req.body and req.headers for authentication:

    • Send a payload (user information) while authenticating using a header (like a token).
    app.post('/user', (req, res) => {
      const token = req.headers.authorization;
      const { username, email } = req.body;
     
      if (token === 'Bearer mytoken') {
        res.json({ message: `User ${username} created with email ${email}` });
      } else {
        res.status(403).json({ message: 'Unauthorized' });
      }
    });
  2. Handling a GET request with req.params and req.query:

    • Fetching a user’s orders with a route parameter for user ID and query parameters for pagination.
    app.get('/user/:id/orders', (req, res) => {
      const { id } = req.params;
      const { page, limit } = req.query;
     
      res.json({ message: `Fetching orders for user ${id}, page ${page}, limit ${limit}` });
    });

Combining Express & Axios Examples:

  1. Passing query strings using Axios with req.query:
app.get('/search', (req, res) => {
  const { keyword, limit } = req.query;
  axios.get(`https://api.example.com/search`, { params: { keyword, limit } })
    .then(response => res.json(response.data))
    .catch(error => res.status(500).json({ error: error.message }));
});
  1. Sending data using req.body and setting headers:
app.post('/submit-data', (req, res) => {
  const data = req.body;
  axios.post('https://api.example.com/data', data, {
    headers: {
      Authorization: 'Bearer token'
    }
  })
    .then(response => res.json(response.data))
    .catch(error => res.status(500).json({ error: error.message }));
});

By understanding and leveraging the different parameters available in Express callback functions, we can effectively handle various aspects of incoming requests, such as route parameters (req.params), query strings (req.query), request bodies (req.body), and headers (req.headers). Each has its own specific role, and combined together, they allow us to create powerful and flexible APIs.