Express: Request Parameters
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:
1 | ?name=flavio |
Multiple query parameters can be added using the ampersand &
. For example:
1 | ?name=flavio&age=35 |
To retrieve the query string values in Express, you can access the Request.query
object directly. Here’s an example:
1 | const express = require('express'); |
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:
1 | for (const key in req.query) { |
You can also access individual query parameters directly:
1 | req.query.name // flavio |
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:
1 | const express = require('express'); |
If the data was sent using Content-Type: application/x-www-form-urlencoded
, you need to use the express.urlencoded()
middleware:
1 | const express = require('express'); |
Regardless of the middleware used, you can access the data through req.body
. Here’s an example:
1 | app.post('/form', (req, res) => { |
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.
tags: [“Express”, “Request parameters”, “GET query string”, “POST query string”, “express.json()”, “express.urlencoded()”]