HTTP handlers read the request (method, URL, headers, body) and write the response (status, headers, body). REST APIs lean on JSON bodies and standard status codes.
Common req properties (Express)
req.params— route placeholdersreq.query— query string (?page=2)req.body— parsed JSON/form (after middleware)req.headers— Authorization, Content-Type, etc.
Response helpers
res.status(404).json({ error: 'Not found' })res.redirect(302, '/login')res.set('Cache-Control', 'no-store')
Content negotiation
Set Content-Type accurately—application/json for APIs, text/html for pages. Wrong types break clients and caching.
Important interview questions and answers
- Q: params vs query?
A: Params are path segments (/users/5); query is optional filters (?sort=name)—both need validation. - Q: When send 204?
A: Success with no body—common for DELETE operations.
Self-check
- Where does JSON body data appear after express.json()?
- Why set Content-Type for JSON responses?