A comprehensive guide to the properties of the Request object and how to use them effectively.
Request Parameters
The Request object in Express holds crucial information about the HTTP request. Here are the key properties you’ll likely use:
Property | Description |
---|---|
.app | Holds a reference to the Express app object. |
.baseUrl | Represents the base path on which the app responds. |
.body | Holds the data submitted in the request body. However, it must be parsed and populated manually before accessing it. |
.cookies | Contains the cookies sent by the request (requires the cookie-parser middleware). |
.hostname | Represents the hostname as defined in the Host HTTP header. |
.ip | Represents the client IP address. |
.method | Indicates the HTTP method used. |
.params | Contains the route named parameters. |
.path | Represents the URL path. |
.protocol | Represents the request protocol. |
.query | An object containing all the query strings used in the request. |
.secure | True if the request is secure (uses HTTPS). |
.signedCookies | Contains the signed cookies sent by the request (requires the cookie-parser middleware). |
.xhr | True if the request is an XMLHttpRequest. |
Retrieving GET Query String Parameters using Express
The query string appears after the URL path and starts with a question mark ?
. For example:
?name=flavio
Multiple query parameters can be added using the ampersand &
. For example:
?name=flavio&age=35
To retrieve the query string values in Express, you can access the Request.query
object directly. Here’s an example:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
console.log(req.query);
});
app.listen(8080);
The req.query
object is automatically populated with the query parameters. If there are no query parameters, it will be an empty object. You can iterate over it using a for...in
loop to access the key-value pairs:
for (const key in req.query) {
console.log(key, req.query[key]);
}
You can also access individual query parameters directly:
req.query.name // flavio
req.query.age // 35
Retrieving POST Query String Parameters using Express
POST query parameters are sent by HTTP clients, such as forms, when performing a POST request to send data. To access this data, you need to use appropriate middleware based on the data format.
If the data was sent as JSON using Content-Type: application/json
, you should use the express.json()
middleware:
const express = require('express');
const app = express();
app.use(express.json());
If the data was sent using Content-Type: application/x-www-form-urlencoded
, you need to use the express.urlencoded()
middleware:
const express = require('express');
const app = express();
app.use(express.urlencoded({ extended: true }));
Regardless of the middleware used, you can access the data through req.body
. Here’s an example:
app.post('/form', (req, res) => {
const name = req.body.name;
});
Note that older versions of Express required the use of the body-parser
module to process POST data. However, this is no longer necessary starting from Express 4.16, released in September 2017.