HI WELCOME TO SIRIS

Express.js Request Object

Leave a Comment
Express.js Request and Response objects are the parameters of the callback function which is used in Express applications.
The express.js request object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on.
Syntax:
  1. app.get('/', function (req, res) {  
  2.    // --  
  3. })  

Express.js Request Object Properties

The following table specifies some of the properties associated with request object.
IndexPropertiesDescription
1.req.appThis is used to hold a reference to the instance of the express application that is using the middleware.
2.req.baseurlIt specifies the URL path on which a router instance was mounted.
3.req.bodyIt contains key-value pairs of data submitted in the request body. By default, it is undefined, and is populated when you use body-parsing middleware such as body-parser.
4.req.cookiesWhen we use cookie-parser middleware, this property is an object that contains cookies sent by the request.
5.req.freshIt specifies that the request is "fresh." it is the opposite of req.stale.
6.req.hostnameIt contains the hostname from the "host" http header.
7.req.ipIt specifies the remote IP address of the request.
8.req.ipsWhen the trust proxy setting is true, this property contains an array of IP addresses specified in the ?x-forwarded-for? request header.
9.req.originalurlThis property is much like req.url; however, it retains the original request URL, allowing you to rewrite req.url freely for internal routing purposes.
10.req.paramsAn object containing properties mapped to the named route ?parameters?. For example, if you have the route /user/:name, then the "name" property is available as req.params.name. This object defaults to {}.
11.req.pathIt contains the path part of the request URL.
12.req.protocolThe request protocol string, "http" or "https" when requested with TLS.
13.req.queryAn object containing a property for each query string parameter in the route.
14.req.routeThe currently-matched route, a string.
15.req.secureA Boolean that is true if a TLS connection is established.
16.req.signedcookiesWhen using cookie-parser middleware, this property contains signed cookies sent by the request, unsigned and ready for use.
17.req.staleIt indicates whether the request is "stale," and is the opposite of req.fresh.
18.req.subdomainsIt represents an array of subdomains in the domain name of the request.
19.req.xhrA Boolean value that is true if the request's "x-requested-with" header field is "xmlhttprequest", indicating that the request was issued by a client library such as jQuery

Request Object Methods

Following is a list of some generally used request object methods:

req.accepts (types)

This method is used to check whether the specified content types are acceptable, based on the request's Accept HTTP header field.
Examples:
  1. req.accepts('html');  
  2. //=>?html?  
  3. req.accepts('text/html');  
  4. // => ?text/html?  

req.get(field)

This method returns the specified HTTP request header field.
Examples:
  1. req.get('Content-Type');  
  2. // => "text/plain"  
  3. req.get('content-type');  
  4. // => "text/plain"  
  5. req.get('Something');  
  6. // => undefined  

req.is(type)

This method returns true if the incoming request's "Content-Type" HTTP header field matches the MIME type specified by the type parameter.
Examples:
  1. // With Content-Type: text/html; charset=utf-8  
  2. req.is('html');  
  3. req.is('text/html');  
  4. req.is('text/*');  
  5. // => true  

req.param(name [, defaultValue])

This method is used to fetch the value of param name when present.
Examples:
  1. // ?name=sasha  
  2. req.param('name')  
  3. // => "sasha"  
  4. // POST name=sasha  
  5. req.param('name')  
  6. // => "sasha"  
  7. // /user/sasha for /user/:name   
  8. req.param('name')  
  9. // => "sasha"  

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.