Request & Response Objects

Node js 8 min min read Updated: Mar 30, 2026 Intermediate
Request & Response Objects
Intermediate Topic 4 of 10

Request & Response Objects in Express.js (req & res)

In Express.js, the req (request) and res (response) objects are the foundation of handling HTTP communication. Every time a client sends a request to the server, Express provides these two objects to process the request and send back a response.

Understanding how req and res work is essential for building APIs, handling user input, and sending data back to the client.

Key Concept: The req object contains incoming request data, while the res object is used to send responses back to the client.

What is the Request Object (req)?

The req object represents the HTTP request sent by the client. It contains all the details about the request such as parameters, query strings, headers, and body data.

Common Properties of req

  • req.params – Route parameters (e.g., /users/:id)
  • req.query – Query string data (e.g., ?name=Rahul)
  • req.body – Data sent in the request body (POST/PUT)
  • req.headers – Request headers
  • req.method – HTTP method (GET, POST, etc.)
  • req.url – Requested URL

What is the Response Object (res)?

The res object is used to send a response back to the client. It allows you to return data, status codes, and headers.

Common Methods of res

  • res.send() – Send a text or HTML response
  • res.json() – Send JSON data
  • res.status() – Set HTTP status code
  • res.redirect() – Redirect to another URL
  • res.end() – End the response

Basic Example

javascript app.post("/data",(req,res)=>{ res.json({message:"Success"}); });

In this example:

  • The route handles a POST request to /data
  • req contains incoming request data
  • res.json() sends a JSON response to the client
Output

{ "message": "Success" }

Working with req.params

javascript app.get("/user/:id",(req,res)=>{ res.send(req.params.id); });

Example:

Output

URL: /user/101 → Response: 101

Working with req.query

javascript app.get("/search",(req,res)=>{ res.send(req.query.name); });
Output

URL: /search?name=Rahul → Response: Rahul

Working with req.body

To access request body data, you need middleware like express.json().

javascript app.use(express.json()); app.post("/user",(req,res)=>{ res.send(req.body.name); });

Setting Status Codes

javascript res.status(200).json({message:"OK"}); res.status(404).send("Not Found");

Sending Different Responses

Send Text

javascript res.send("Hello World");

Send JSON

javascript res.json({name:"Rahul"});

Send HTML

javascript res.send("<h1>Hello</h1>");

Real-World Use Cases

  • Building REST APIs
  • Handling form submissions
  • Processing user input
  • Returning JSON responses to frontend apps

Best Practices

  • Always validate request data
  • Use proper status codes
  • Handle errors gracefully
  • Avoid sending multiple responses

Common Mistakes

  • Not using express.json() for body parsing
  • Sending response multiple times
  • Ignoring validation of input data

Conclusion

The req and res objects are the backbone of any Express.js application. They allow you to receive data from the client and send responses effectively.

Mastering these objects is essential for building APIs, handling user interactions, and creating scalable backend applications.

Quick Summary: req handles incoming data, and res sends responses back to the client in Express.js.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators