The req Object
req represents the incoming HTTP request — its method, URL, headers, route parameters, query string, and (once parsed by middleware) its body.
Useful req Properties
| Property | Contains |
|---|---|
| req.params | Named route parameters, like :id |
| req.query | The query string, parsed into an object |
| req.body | The parsed request body (requires express.json() middleware) |
| req.headers | HTTP request headers |
| req.method | The HTTP method (GET, POST, etc.) |
Reading From req
Try it yourself — edit and run
Console Output
Click “Run” to see the console output here.
The res Object
res represents the response being built and sent back. Its methods are chainable, so you'll commonly see them combined in a single expression.
Useful res Methods
| Method | Effect |
|---|---|
| res.status(code) | Set the HTTP status code |
| res.json(data) | Send a JSON response |
| res.send(data) | Send a response, inferring the content type |
| res.redirect(url) | Redirect to a different URL |
| res.set(header, value) | Set a response header |
Chaining Response Methods
Try it yourself — edit and run
Console Output
Click “Run” to see the console output here.
Best Practice
Use res.json() rather than res.send() when returning structured data — it explicitly sets the correct Content-Type header and consistently serializes the response, avoiding ambiguity about what's being sent.