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 dataapp.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 Axiosaxios.post('/create-user', { username: 'john', email: 'john@example.com' });
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.
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:
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.
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:
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).
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.