We now have a youtube channel. Subscribe!

Node.js | Request Object

Node.js | Request Object


Hello folks! welcome back to a new edition of our tutorial on Node.js. In this section of our tutorial on Node.js, we will be studying about Node.js Request Object.

The req object represents the HTTP request and has properties for request query strings, parameters, body, HTTP headers, etc.

Request Object Properties

The following table below is the list of a few properties associated with request object -

Sr.No.Properties & Description
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.



Request Object Methods

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

Following is a simple example -

// Accept: text/html
req.accepts('html');
// => "html"

// Accept: text/*, application/json
req.accepts('html');

// => "html"
req.accepts('text/html');
// => "text/html"

req.get(field)

req.get(field)

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

Example

Following is a simple example -

req.get('Content-Type');
// => "text/plain"

req.get('content-type');
// => "text/plain"

req.get('Something');
// => undefined

req.is(type)

req.is(type)

This Node.js request object method retuns true if the incoming request's "Content-type" HTTP header field matches the MIME types specified by the type parameter.

Example

Following is a simple example -

// With Content-Type: text/html; charset=utf-8
req.is('html');
req.is('text/html');
req.is('text/*');
// => true

req.param(name [, defaultValue])

req.param(name [, defaultValue])

This returns the value of param name when present.

Example

Following is a simple example -

// ?name=tobi
req.param('name')
// => "tobi"

// POST name=tobi
req.param('name')
// => "tobi"

// /user/tobi for /user/:name 
req.param('name')
// => "tobi"


Alright guys! This is where we are going to be rounding up for this tutorial. In our next tutorial, we will be studying about Node.js Response Object.

Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.
© 2022 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain