Node.js Course
Node.js
/
Beginner

Request & Response Objects

Definition

The enhanced objects provided by Express representing the HTTP request (`req`) and the HTTP response (`res`). Express adds powerful utility methods on top of the raw Node.js objects.

Explain Like I'm New

Express takes the raw, difficult-to-use Node.js `req` and `res` objects and gives them superpowers. Instead of manually turning objects into JSON strings and configuring headers, you just write `res.json(user)`. Express does all the heavy lifting instantly.

Real World Example

Using `req.ip` to find the user's IP address to block a hacker, or using `res.download('report.pdf')` which automatically prompts the user's browser to download a file with one line of code.

Common Use Cases

  • •Reading client data
  • •Formatting JSON responses
  • •Setting cookies

Interactive Example

Loading...
Console output will appear here...

Interview Questions

basic

  • What Express method sends a JSON response?

intermediate

  • Where does data from an HTML form POST request live in the `req` object?

Flash Cards

Question

What method sends JSON?

Click to reveal answer
Answer

`res.json({ data: 'hello' })`.

Question

Where does POST data live?

Click to reveal answer
Answer

In `req.body`. (Note: you must use the `app.use(express.json())` middleware for this object to be populated, otherwise it will be undefined!).