Sunday, June 7, 2020

ExpressJs Request object

ExpressJs Request object

Express.js req and res objects are used to represent the parameters of the callback function in Express applications. The request object is used to represent the HTTP request. This object has many properties for accessing request query string, the request parameters, body, HTTP headers, and much more. The request object is generally represented by the req object.

The general syntax of a callback function is,

app.get('/', function (req, res) {  
   // Write code here 

The request object has many properties and methods.


This property carries a reference to the instance of the ExpressJS application that is using the middleware.


This property is very much like the mountpath property of the app object, excluding app.mountpath returns the matched path pattern/s.


It holds key-value pairs of data submitted in the request body. It is undefined by default, and it is populated with data when we use body-parsing middleware such as body-parser.


This property is an object that holds cookies(read JavaScript cookies) sent by the request can be accessed with the cookie-parser middleware.


req.fresh represents that the request is "fresh." It is just the opposite of req.stale.


req.hostname represents the hostname from the "Host" HTTP header.


req.ip represents the remote IP address of the request.


When the trust proxy setting is true, req.ips property has an array of IP addresses provided in the ?x-forwarded-for? request header.


req.originalurl property is similar to req.url, except, req.originalurl keeps the actual request URL. This allows us to rewrite req.url independently for internal routing.


req.params represents an object holding the properties mapped to the named route ? parameters?. For instance, if we have the route /user/:id, then the "id" property is made available as By default, it is set to {}.


req.path represents the path component of the request URL.



This property represents request protocol string, It can be "HTTP" or "HTTPS".


This object carries properties of each query string parameter in the available in the route.


req.route represents currently-matched route, basically a string.         

It is a boolean value. That is true if a TLS connection is established, otherwise false.


It is used to represent if the request is "stale", It is just the opposite of req.fresh property.


req.subdomains provide an array containing the subdomains in the domain name of the request.


This is a boolean value, that returns true if the request's "x-requested-with" header field is "xmlhttprequest", showing that this request was sent by a client library like jQuery, etc.

Request Object methods

The methods provided by the request object are,


This method can be used to check if the provided content types can be accepted or not, based on the request accept HTTP header field. The method returns false (the application responds with 406 "Not Acceptable") if no match is found, otherwise it returns the best match.

The type value can be a single MIME type string (for example “application/json”), an extension name such as “json”, a comma-delimited list, or an array. For a list or array, the method returns the best match (if any).

For example,

// text/html
// => "html"

// text/*, application/json
// => "html"
// => "text/html"
req.accepts(['json', 'text'])
// => "json"
// => "application/json"

// text/*, application/json
// => false

// text/*;q=.5, application/json
req.accepts(['html', 'json'])
// => "json"

req.acceptsCharsets(charset [, ...])

This method returns the first accepted charset of the provided character sets, on the basis of the request’s Accept-Charset HTTP header field. It returns false if none of the provided charsets is acceptable.

req.acceptsEncodings(encoding [, ...]) 

This method returns the first accepted encoding of the provided encodings, on the basis of the request’s Accept-Encoding HTTP header field. It returns false if none of the provided encodings is acceptable.

req.acceptsLanguages(lang [, ...]) 

This method returns the first accepted language of the provided languages, on the basis of the request’s Accept-Language HTTP header field. It returns false if none of the provides languages is acceptable.


req.get(field) method returns the provided HTTP request header field. This match is made case-insensitively. 

This method returns the matching content-type if the incoming request’s “Content-Type” HTTP header field matches the MIME type provided by the type parameter. If the request has no body, returns null. Otherwise, it returns false.

// If Content Type is text/html; charset=utf-8, True'html') // => 'html''text/html') // => 'text/html''text/*') // => 'text/*'

// If the Content Type is application/json ,True'json') // => 'json''application/json') // => 'application/json''application/*') // => 'application/*'