Express.js Request Object

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.

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

Getting the http Methods 
  // creating a http server with express
  // - using conditional statement
  var express = require("express");
  var app = express();

  var http = require("http");
  // console.log(http.METHODS);
  console.log(http.STATUS_CODES);

In Express.js, the request object (req) represents the HTTP request made by the client to the server. It contains various properties and methods that allow you to access request-related information, such as headers, body, query parameters, cookies, and more.

Commonly Used Properties of req

Here are the most frequently used properties:

req.params

In Express.js, req.params is an object containing the route parameters mapped to their respective values. Route parameters are defined in the route path by using a colon (:) followed by a name, like /user/:id

Example:

   const express = require('express');
  const app = express();

  var users =[
      {id:1, name:"ramesh",course:"rjs"},
      {id:2, name:"suresh",course:"nodejs"},
      {id:3, name:"kishore",course:"ajs"}
  ]

  app.get('/users/:id', (req, res) => {
    const userId = req.params.id; // Access route parameter
    res.send(`User ID is: ${userId}`);
   
  });

  app.listen(3000, () => {
    console.log('Server running on port 3000')}
  );


req.query
Contains query string parameters as key-value pairs.
Example:
  // Request: GET /search?term=express
  app.get('/search', (req, res) => {
    console.log(req.query.term); // 'express'
  }); req.body
Contains the body data of the request. This requires middleware such as express.json() or express.urlencoded() to parse the body.
Example
app.use(express.json());
  app.post('/submit', (req, res) => {
      console.log(req.body); // Parsed body object
  }); req.headers
Contains the headers sent by the client as an object.
Example:

  app.get('/', (req, res) => {
    console.log(req.headers['user-agent']); // Access the User-Agent header
  });

req.method
The HTTP method used in the request (GET, POST, etc.).
Example:

  app.all('*', (req, res) => {
    console.log(req.method); // Logs the HTTP method
  });

req.url
The URL of the request (excluding the host).
Example:

  app.get('*', (req, res) => {
    console.log(req.url); // Logs the URL path
  });

req.ip
The IP address of the client.
Example:

  app.get('/', (req, res) => {
    console.log(req.ip); // Logs the client's IP address
  });

req from server

    // creating a http server with express
    // - using conditional statement
    var express = require("express");
    var app = express();

    // var http = require("http");
    // // console.log(http.METHODS);
    // console.log(http.STATUS_CODES);

    // req from header server
    app.get("/",(req,res)=>{
      console.log(req.headers);
      res.status(404).end();
    })

    // http server in express js
    app.listen(3000,(err)=>{
      if(err){
        console.log("error!!..");
        return;
      }else{
        console.log("port is running");
      }
    })