Example Object request of express

The req object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on.

Request Object Properties

1.req.app: This property holds a reference to the instance of the express application that is using the middleware.

2. req.baseUrl: The URL path on which a router instance was mounted.

3. req.body : 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.cookies: When using cookie-parser middleware, this property is an object that contains cookies sent by the request.

5. req.fresh: Indicates whether the request is "fresh." It is the opposite of req.stale.

6. req.hostname: Contains the hostname from the "Host" HTTP header.

7.req.ip: The remote IP address of the request.

8. req.ips: When the trust proxy setting is true, this property contains an array of IP addresses specified in the “X-Forwarded-For” request header.

9. req.originalUrl: This 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.params: An 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.path: Contains the path part of the request URL.

12.req.protocol: The request protocol string, "http" or "https" when requested with TLS.

13. req.query: An object containing a property for each query string parameter in the route.

14. req.route: The currently-matched route, a string.

15. req.secure: A Boolean that is true if a TLS connection is established.

16. req.signedCookies: When using cookie-parser middleware, this property contains signed cookies sent by the request, unsigned and ready for use.

17. req.stale: Indicates whether the request is "stale," and is the opposite of req.fresh.

18. req.subdomains: An array of subdomains in the domain name of the request.

19. req.xhr: A 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.

Example:

 

var express = require('express');

var app = express();

app.get('/home', function (req, res) {

    console.log("req.app------------------>");

    console.log(req.app);

    console.log("req.baseUrl-------------->");

    console.log(req.baseUrl);

    console.log("req.body------------------>");

    console.log(req.body);

    console.log("req.cookies---------------->");

    console.log(req.cookies);

    console.log("req.fresh------------------>");

    console.log(req.fresh);

    console.log("req.hostname--------------->");

    console.log(req.hostname);

    console.log("req.ip--------------------->");

    console.log(req.ip);

    console.log("req.ips--------------------->");

    console.log(req.ips);

    console.log("req.originalUrl------------->");

    console.log(req.originalUrl);

    console.log("req.params------------------>");

    console.log(req.params);

    console.log("req.path-------------------->");

    console.log(req.path);

    console.log("req.protocol---------------->");

    console.log(req.protocol);

    console.log("req.query------------------->");

    console.log(req.query);

    console.log("req.route------------------->");

    console.log(req.route);

    console.log("req.secure------------------>");

    console.log(req.secure);

    console.log("req.signedCookies----------->");

    console.log(req.signedCookies);

    console.log("req.stale------------------->");

    console.log(req.stale);

    console.log("req.subdomains-------------->");

    console.log(req.subdomains);

    console.log("req.xhr--------------------->");

    console.log(req.xhr);

   res.send('Hello World');

});

 

var server = app.listen(8080, function () {

   var host = server.address().address;

   var port = server.address().port;

  

   console.log("Server listening at http://%s:%s", host, port);

});

 

Run :

 

C:\Nodejs>node express_object_request.js

Server listening at http://:::8080

 

Go browser go http://localhost:8080/

 

Hello world

 

Log in server:

 

C:\Nodejs>node express_object_request.js

Server listening at http://:::8080

req.app------------------>

{ [Function: app]

  domain: undefined,

  _events: { mount: [Function: onmount] },

  _maxListeners: undefined,

  setMaxListeners: [Function: setMaxListeners],

  getMaxListeners: [Function: getMaxListeners],

  emit: [Function: emit],

  addListener: [Function: addListener],

  on: [Function: addListener],

  prependListener: [Function: prependListener],

  once: [Function: once],

  prependOnceListener: [Function: prependOnceListener],

  removeListener: [Function: removeListener],

  removeAllListeners: [Function: removeAllListeners],

  listeners: [Function: listeners],

  listenerCount: [Function: listenerCount],

  eventNames: [Function: eventNames],

  init: [Function: init],

  defaultConfiguration: [Function: defaultConfiguration],

  lazyrouter: [Function: lazyrouter],

  handle: [Function: handle],

  use: [Function: use],

  route: [Function: route],

  engine: [Function: engine],

  param: [Function: param],

  set: [Function: set],

  path: [Function: path],

  enabled: [Function: enabled],

  disabled: [Function: disabled],

  enable: [Function: enable],

  disable: [Function: disable],

  acl: [Function],

  bind: [Function],

  checkout: [Function],

  connect: [Function],

  copy: [Function],

  delete: [Function],

  get: [Function],

  head: [Function],

  link: [Function],

  lock: [Function],

  'm-search': [Function],

  merge: [Function],

  mkactivity: [Function],

  mkcalendar: [Function],

  mkcol: [Function],

  move: [Function],

  notify: [Function],

  options: [Function],

  patch: [Function],

  post: [Function],

  propfind: [Function],

  proppatch: [Function],

  purge: [Function],

  put: [Function],

  rebind: [Function],

  report: [Function],

  search: [Function],

  subscribe: [Function],

  trace: [Function],

  unbind: [Function],

  unlink: [Function],

  unlock: [Function],

  unsubscribe: [Function],

  all: [Function: all],

  del: [Function],

  render: [Function: render],

  listen: [Function: listen],

  request: IncomingMessage { app: [Circular] },

  response: ServerResponse { app: [Circular] },

  cache: {},

  engines: {},

  settings:

   { 'x-powered-by': true,

     etag: 'weak',

     'etag fn': [Function: generateETag],

     env: 'development',

     'query parser': 'extended',

     'query parser fn': [Function: parseExtendedQueryString],

     'subdomain offset': 2,

     'trust proxy': false,

     'trust proxy fn': [Function: trustNone],

     view: [Function: View],

     views: 'C:\\Nodejs\\views',

     'jsonp callback name': 'callback' },

  _eventsCount: 1,

  locals:

   { settings:

      { 'x-powered-by': true,

        etag: 'weak',

        'etag fn': [Function: generateETag],

        env: 'development',

        'query parser': 'extended',

        'query parser fn': [Function: parseExtendedQueryString],

        'subdomain offset': 2,

        'trust proxy': false,

        'trust proxy fn': [Function: trustNone],

        view: [Function: View],

        views: 'C:\\Nodejs\\views',

        'jsonp callback name': 'callback' } },

  mountpath: '/',

  _router:

   { [Function: router]

     params: {},

     _params: [],

     caseSensitive: false,

     mergeParams: undefined,

     strict: false,

     stack: [ [Object], [Object], [Object] ] } }

req.baseUrl-------------->

 

req.body------------------>

undefined

req.cookies---------------->

undefined

req.fresh------------------>

false

req.hostname--------------->

localhost

req.ip--------------------->

::1

req.ips--------------------->

[]

req.originalUrl------------->

/home

req.params------------------>

{}

req.path-------------------->

/home

req.protocol---------------->

http

req.query------------------->

{}

req.route------------------->

Route {

  path: '/home',

  stack:

   [ Layer {

       handle: [Function],

       name: '<anonymous>',

       params: undefined,

       path: undefined,

       keys: [],

       regexp: /^\/?$/i,

       method: 'get' } ],

  methods: { get: true } }

req.secure------------------>

false

req.signedCookies----------->

undefined

req.stale------------------->

true

req.subdomains-------------->

[]

req.xhr--------------------->

false

 

Request Object Methods

1. req.accepts(types)

req.accepts(types): This method checks if the specified content types are acceptable, based on the request’s Accept HTTP header field.

Example:

 

// Accept: text/html

req.accepts('html');

// => "html"

// Accept: text/*, application/json

req.accepts('html');

// => "html"

req.accepts('text/html');

// => "text/html"

 

2.req.get(field)

req.get(field): This method returns the specified HTTP request header field.

Example:

 

req.get('Content-Type');

// => "text/plain"

req.get('content-type');

// => "text/plain"

req.get('Something');

// => undefined

 

3. req.is(type)

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.

Example:

 

// With Content-Type: text/html; charset=utf-8

req.is(‘html’);

req.is(‘text/html’);

req.is(‘text/*’);

// => true

 

4. req.param(name [, defaultValue])

req.param(name [, defaultValue]): This method returns the value of param name when present.

Example:

 

// ?name=tobi

req.param('name')

// => "tobi"

 

// POST name=tobi

req.param('name')

// => "tobi"

 

// /user/tobi for /user/:name

req.param('name')

// => "tobi"