Lesson 11 / 23
The res Object
Send text, JSON, status codes, or redirect the client.
send, json, status, redirect
res.send() sends text/HTML/buffer, res.json() sends JSON, res.status(code) sets the HTTP status, res.redirect(url) sends a 3xx redirect.
Chaining
Most res methods return res, so they chain naturally.
app.post('/items', (req, res) => {
res.status(201).json({ id: 1, name: 'Widget' });
});
app.get('/old-path', (req, res) => {
res.redirect(301, '/new-path');
});Respond exactly once
Calling res.send/res.json twice on one request throws ERR_HTTP_HEADERS_SENT — always return after sending inside a branch.