Skip to main content

The Request object

Properties

req.url

A string containing the request URL.

req.urlObj

The URL object corresponding to the request URL.

req.query

Request query string parameters.

req.params

An object containing properties mapped to any named route "parameters", if the extractRequestParameters configuration option is enabled.

req.cookies

A Map containing request cookies, if the parseCookie configuration option is enabled.

req.headers

Request headers.

req.path

The path part of the request URL.

req.ip

The remote IP address of the request

req.protocol

The request protocol string: either http or (for TLS requests) https.

req.secure

A boolean value that is true if a TLS connection is established.

req.subdomains

An array of subdomains in the domain name of the request.

req.hostname

The hostname derived from the Host HTTP header.

req.geo

A geolocation dictionary corresponding to the IP address of the downstream client.

Methods

req.waitUntil()

Tells the host environment that work is ongoing until the promise settles, and it shouldn't terminate the application if it wants that work to complete.

Working with the request body

expressly can parse the body of a request in multiple ways.

As plain text

router.post("/submit", async (req, res) => {
let body = await req.text();

res.send(`You posted: "${body}"`)
})

As JSON

router.post("/submit", async (req, res) => {
// Parse body as JSON
let body = await req.json();

// Check if the body contains the key "item"
if ("item" in body) {
res.send(`item: ${body.item}`)
} else {
res.send("You must include item in your body!")
}
})

As an ArrayBuffer

router.post("/submit", async (req, res) => {
// Parse body into an ArrayBuffer
let body = await req.arrayBuffer();

console.debug(body);
})