Express.js provides a request object (req) that contains information about the incoming HTTP request, such as request parameters, query strings, headers, cookies, and other request details. It also provides methods for handling content negotiation, request headers, and other common HTTP operations.
Express.js Request Properties
- Express.js req.app Property: The req.app property holds a reference to the instance of the Express application that is using the middleware.
- Express.js req.baseUrl Property: The req.baseUrl property returns the URL path on which the current router was mounted.
- Express.js req.body Property: The req.body property contains data submitted in the request body. It is populated by body-parsing middleware such as express.json() and express.urlencoded().
- Express.js req.cookies Property: The req.cookies property contains cookies sent by the client in an HTTP request. It is populated by the cookie-parser middleware.
- Express.js req.fresh Property: The req.fresh property returns true when the client's cached response is considered fresh according to the request and response cache headers. Otherwise, it returns false.
- Express.js req.hostname Property: The req.hostname property contains the hostname derived from the Host HTTP header.
- Express.js req.ip Property: The req.ip property contains the remote IP address of the request.
- Express.js req.ips Property: The req.ips property contains an array of IP addresses provided through the X-Forwarded-For header when the trust proxy setting is enabled.
- Express.js req.method Property: The req.method property contains the HTTP method of the request, such as GET, POST, PUT, or DELETE.
- Express.js req.originalUrl Property: The req.originalUrl property contains the original request URL, including the pathname and query string, before any internal changes to req.url.
- Express.js req.params Property: The req.params property is an object containing values captured from named route parameters.
- Express.js req.path Property: The req.path property contains the pathname portion of the request URL.
- Express.js req.protocol Property: The req.protocol property contains the protocol used for the request, such as http or https.
- Express.js req.query Property: The req.query property contains the query string parameters parsed from the request URL.
- Express.js req.route Property: The req.route property contains information about the currently matched route.
- Express.js req.secure Property: The req.secure property is true when the request is using a TLS connection and false otherwise.
- Express.js req.signedCookies Property: The req.signedCookies property contains signed cookies sent by the client. It is populated by the cookie-parser middleware when a secret is configured.
- Express.js req.stale Property: The req.stale property returns true when the client's cached response is stale and false when it is fresh.
- Express.js req.subdomains Property: The req.subdomains property contains an array of subdomains in the request hostname. The subdomain offset setting determines which hostname segments are considered subdomains.
- Express.js req.xhr Property: The req.xhr property returns true when the request contains an X-Requested-With: XMLHttpRequest header.
Express.js Request Methods:
- Express.js req.accepts() Function: The req.accepts() function checks whether the specified content types are acceptable based on the request's Accept HTTP header. It returns the best matching content type, or false if none of the specified types is acceptable.
- Express.js req.acceptsCharsets() Function: The req.acceptsCharsets() function checks whether the specified character sets are acceptable based on the request's Accept-Charset HTTP header. It returns the first matching character set, or false if none of the specified character sets is acceptable.
- Express.js req.acceptsEncodings() Function: The req.acceptsEncodings() function checks whether the specified content encodings are acceptable based on the request's Accept-Encoding HTTP header. It returns the first matching encoding, or false if none of the specified encodings is acceptable.
- Express.js req.acceptsLanguages() Function: The req.acceptsLanguages() function checks whether the specified languages are acceptable based on the request's Accept-Language HTTP header. It returns the first matching language, or false if none of the specified languages is acceptable.
- Express.js req.get() Function: The req.get() function returns the value of the specified HTTP request header. Header names are case-insensitive.
- Express.js req.is() Function: The req.is() function checks whether the incoming request's Content-Type matches the specified MIME type. It returns the matching type when found, or false when there is no match.