How to use Request Object in Express Nodejs

In this article, we’ll learn useful information about request objects in Express and their properties.

I. What is Request Object in Express?

When the user (client) accesses a route, the user sends the Express application a Request.

The request object, often abbreviated as req, represents the HTTP request property.

We can access any data sent with the body, such as URL parameters, query strings, and HTTP headers.

II. Common Request object properties

req.paramsAn object containing properties mapped to the named route “parameters”
req.queryAn object containing a property for each query string parameter in the route.
req.bodyContains key-value pairs of data submitted in the request body
req.protocolContains the request protocol string (http or https).
req.hostnameContains the hostname derived from the Host HTTP header.
req.pathContains the path part of the request URL.
req.originalUrlContains the entire request url.
req.subdomainsAn array of subdomains in the domain name of the request.
req.headerAn HTTP header that can be used in an HTTP response.
req.cookiesAn object containing cookies sent by the request
res.ipContains the remote IP address of the request.

III. Request Object usage and examples

1. Access user-submitted data

There are a few ways for us to receive data from users. For example, we can use req.params, req.query, and req.body.


// GET

app.get("user/:id", (req, res) => {
  console.log(; // "123"


// GET

app.get("user/", (req, res) => {
  console.log(req.query.userID); // "123"
  console.log(req.query.action); // "submit"


// POST {username: "anna", password: "1234"}

app.get("login/", (req, res) => {
  console.log(req.body.username); // "anna"
  console.log(req.body.password); // "1234"

2. Access the URL values

We can access an object that stores URL information such as protocol, hostname, path, and subdomains with a res object to handle URL-related tasks.


app.get("/search", (req, res) => {
  console.log(req.protocol); // "https"
  console.log(req.hostname); // ""
  console.log(req.path); // "/search"
  console.log(req.originalUrl); // "/keyword=express"
  console.log(req.subdomains); // "["learn"]"

3. Access Header values

In addition to sending data and users, the browser also sends headers containing additional information such as content-type, content-length, and other information."/login", (req, res) => {

4. Access cookies value

When using cookie-parse middleware, the req.cookies object will include the user’s cookies.

If the request contains no cookies, it defaults to {}."/login", (req, res) => {