DEV Community

Cover image for deciphering The "req" Object in Node.Js
Jhaemis-hack
Jhaemis-hack

Posted on

deciphering The "req" Object in Node.Js

Have you also been wandering what Exactly does "req" and "res" do in your express application, What are the methods I can utilize with it. Have you also been taunted with the fact that we do not only have req.body, but also req.params... apart from this two, what other methods does it have? Well, you are not alone (...Obviously). In this post, I will be helping you to decipher what exactly "req" and "res" does in your express app, and methods associated with REQUEST object.

The 'req' (request) object in Express JS is used to represent the incoming HTTP request that consists of data like parameters, query strings, request body and so on.

Some Other Usefulness Include:

  1. Accessing the Request Data: By using the req object, we can access the various components of the incoming HTTP request which consists of data, headers, parameters, etc.

  2. Middleware Interaction: middleware functions can change and modify the request object by allowing various tasks of logging and authentication.

  3. Routing: By implementing the dynamic routes in the application, the req object can be used as it captures the URL parameters and also allows it to respond dynamically based on the client's input.

While, on the Other hand, "res" (Response) object is used to send the HTTP response to the client which allows the modification of headers and consists of status codes, and resources.

Few of it's Usefulness include:

  1. Send Responses: The res object is used to send the HTP responses to the client which includes the resource, data, status codes, and headers.

  2. Error Handling: we can send the error responses which is important for handling the errors and providing feedback to the client.

  3. Content Modification: We can also set the custom response headers, status codes, and content through which the response control can be managed by us.

Now, let's Jump to the Main Focus of this post, REQUEST METHODS!

If you have been following along, you should now have a little glimpse of what "req" is and does in your code. Howbeit, the question still stands, are there any other methods apart from req.body and req.params? I will begin to dig that up for you now.

Methods Associated with REQ. Object:
Majorly, there are four methods directly associated with req object. They are:

  1. req.body
  2. req.params
  3. req.query
  4. req.headers

However, there are also some few properties often confused to be part of the unique methods inbuilt with req object. such include:
'req.method', 'req.url', 'req.cookies', 'req.ip', ...

req.body:

This method allows us to access all the data sent from the front end to the backend by the client in the body of a POST, PUT, or PATCH request. Hence requires a middleware, like express.json() or bodyParser.json() to parse it.

app.post('/login', (req, res) => {
    console.log(req.body); // Logs user credentials sent in the body
});

Enter fullscreen mode Exit fullscreen mode

req.query:

Stores, as a subsidiary of req.body, query parameters right after ? joined to the URL. Often used for filtering or searching datas.

// URL: /search?name=John
app.get('/search', (req, res) => {
    console.log(req.query.name); // Outputs 'John'
});

Enter fullscreen mode Exit fullscreen mode

req.params:

Holds route parameters defined in the URL paths. It is often indicated by a : symbol. It is typically used for dynamic routes. It will automatically get whatsoever is placed in that position in the URL path.

// URL: /user/123
app.get('/user/:id', (req, res) => {
    console.log(req.params.id); // Outputs '123'
});

Enter fullscreen mode Exit fullscreen mode

req.headers:

this method Contains headers sent by the client, often used for authentication or content negotiation.

app.get('/data', (req, res) => {
    console.log(req.headers['authorization']); // Outputs the auth token
});

Enter fullscreen mode Exit fullscreen mode

I hope you can boast yourself of understanding the functionality of "res" and "req" in your express app now.
Thank you for stopping by.

Top comments (0)