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.

Properties


req.app               


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


req.baseurl        


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


req.body            


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.


req.cookies       


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            


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


req.hostname  


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


req.ip   


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


req.ips 


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   


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        


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 req.params.id. By default, it is set to {}.


req.path             


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

 

req.protocol      


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


req.query           


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


req.route           


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


req.secure         


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


req.stale             


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


req.subdomains              


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


req.xhr


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,

req.accepts(types)


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
req.accepts('html')
// => "html"

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

// text/*, application/json
req.accepts('image/png')
req.accepts('png')
// => 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) 

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

req.is(type) 


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
req.is('html') // => 'html'
req.is('text/html') // => 'text/html'
req.is('text/*') // => 'text/*'

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