Express res Object
What you’ll learn
- How Express builds and exposes the
resobject. - Which response methods are used most in handlers.
- How status, headers, and body methods work together.
- How to avoid sending duplicate responses.
Common res properties and methods
javascript
res.status(code)
res.send(body)
res.json(data)
res.set(field, value)
res.get(field)
res.redirect(path)1
Return JSON with status code
javascript
app.get('/health', function (req, res) {
res.status(200).json({
ok: true,
service: 'api'
});
});2
Set a header and redirect
javascript
app.get('/legacy', function (req, res) {
res.set('X-Notice', 'Moved route');
res.redirect(301, '/new-home');
});⚠️ Common pitfalls
- Calling multiple final response methods (
send,json,end) in one request flow. - Forgetting
returnafter early error responses. - Trying to set headers after the response has already been sent.
❓ FAQ
res is the outgoing response object used to send data, set headers, and control status codes.
Common methods include res.status(), res.send(), res.json(), res.set(), and res.redirect().
No. You should send exactly one final response per request.
Use res.json() for API responses because it serializes objects and sets JSON content type.
Return after early responses and use clear control flow in async handlers.
Did you know?
The Express res object extends Node’s response object and provides helpers like status, json, send, set, and redirect.
4 people found this page helpful
