Status Codes, Headers, and Content Types
HTTP status codes communicate the result of a request. 2xx indicates success, 3xx redirection, 4xx client-side problems, and 5xx server-side failures.
Headers provide metadata about the request and response. Common headers include Content-Type, Authorization, Cache-Control, Accept, and security-related headers.
Returning an HTTP Error
res.writeHead(404, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify({
error: 'User not found'
}));💡 The server returns a 404 response with a JSON error body.
Key Takeaways
- Use status codes consistently and meaningfully.
- Set
Content-Typeaccording to the response representation. - Do not use
200 OKfor every outcome.
Knowledge Check
Which status code normally means a resource was not found?