🚀 Express.js
Beginner
How do you send different types of responses in Express.js?
Answer
Express provides several response methods on res: res.send() sends a generic response (string, Buffer, or object). res.json(obj) sends a JSON response and automatically sets the Content-Type: application/json header. res.status(code) sets the HTTP status code and is typically chained: res.status(404).json({ message: 'Not found' }). res.sendFile(path) sends a file as the response. res.redirect(url) sends a 302 redirect. res.render(view, data) renders a template (requires a template engine). res.end() sends an empty response. Always call exactly one response method per request — calling multiple will throw an error.