You need to know about req and res objects to build an API in Node.js using Express.js
Tags: javascript, node, beginners, programming
Published: 2022-06-10 00:07:17

## Hi, Devs!

We can have [req](https://expressjs.com/en/5x/api.html#req) and [res](https://expressjs.com/en/5x/api.html#res) objects into Express.js, they are used to handle the `request and response` using **HTTP protocol and its verbs and status code**.

![page not found 404 status code HTTP](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41wabd87q0llbnj9q8ew.png)

Remember! We have some HTTP verbs and they can do some actions and return some _[status code](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)._

- Informational responses (100–199)
- Successful responses (200–299)
- Redirection messages (300–399)
- Client error responses (400–499)
- Server error responses (500–599)

Let's assume a bit code to show Express.js and its response (res) and request (req) object:

```
const express = require('express');
const cors = require('cors');
const app = express();
const bodyParser = require('body-parser')

app.use(cors())
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))

app.post('/example', handleCreate); //save with req.body data
app.get('/example/search/', handleSearch); //find the data
app.get('/example/:id', handleFindById); //find by an id

app.listen(3000, () => {
  console.log('Running on 3000');
});
```

**Let's see the examples:**

`req.body`
It's populated when you use body-parsing middleware such as body-parser. And response here is just send the result from the database.

**endpoint:** app.post('/example', handleCreate);
**http verb:** [POST](https://developer.mozilla.org/pt-BR/docs/Web/HTTP/Methods/POST)
**URL example:** http://localhost:3000/example
Status: [201](https://developer.mozilla.org/pt-BR/docs/Web/HTTP/Status/201)

```
async function handleCreate(req, res) {
  console.log(req.body)
  const {name, age} = req.body
  const result = await database.create(name, age)
  res.status(201).send(result);
}
```

---

`req.params`
if you have the route /example/:id, then the “id” property is available as req.params.name ou using destructuring like below. And here the response is sending the status and the data in JSON format.

**endpoint:** app.get('/example/:id', handleFindById);
**http verb:** [GET](https://developer.mozilla.org/pt-BR/docs/Web/HTTP/Methods/GET)
**URL example:** http://localhost:3000/example/1
Status: [200](https://developer.mozilla.org/pt-BR/docs/Web/HTTP/Status/200)
Response: [res.status](https://expressjs.com/en/5x/api.html#res.status)(200).json(result)

```
async function handleFindById(req, res) {
  const { id } = req.params //or req.params.id
  const result = await database.findById(id)
  res.status(200).json(result);
}
```
---

`req.query`
This property is an object containing a property for each query string parameter in the route, as well using when we have more than one parameter to share on url, because we can get the value of _?name=Calaca&age=32&live=Brazil&leve=senior_. And the response  here is sending status 200 and result from database.

**endpoint:** app.get('/example/search', handleSearch);
**http verb:** [GET](https://developer.mozilla.org/pt-BR/docs/Web/HTTP/Methods/GET)
**URL example:** http://localhost:3000/example/search?name=Calaca
Status response: [200](https://developer.mozilla.org/pt-BR/docs/Web/HTTP/Status/200)

```
async function handleSearch(req, res) {
  const {name} = req.query
  const result = await database.findByName(name)
  res.status(200).send(result);
}
```

`req.headers`


Simple and effective.

**Contacts**
Email: luizcalaca@gmail.com
Instagram: https://www.instagram.com/luizcalaca
Linkedin: https://www.linkedin.com/in/luizcalaca/
Twitter: https://twitter.com/luizcalaca