What is HTTP status codes and which ones are most important in Node.js APIs?
Answer
HTTP status codes communicate the result of a request. Essential codes for REST APIs: 2xx (Success): 200 OK (general success), 201 Created (resource created — return Location header), 204 No Content (success, no body — used for DELETE). 3xx (Redirection): 301 Moved Permanently, 302 Found (temporary redirect). 4xx (Client Error): 400 Bad Request (invalid input — validation failure), 401 Unauthorized (not authenticated — missing/invalid credentials), 403 Forbidden (authenticated but not authorized), 404 Not Found (resource doesn't exist), 409 Conflict (duplicate resource — unique constraint violation), 422 Unprocessable Entity (semantically invalid — often used for validation), 429 Too Many Requests (rate limited). 5xx (Server Error): 500 Internal Server Error (unexpected server error — log the details, show generic message to client), 502 Bad Gateway (upstream service error), 503 Service Unavailable (overloaded or maintenance). Return consistent error response shapes with status code, error code, and human-readable message: { "error": "RESOURCE_NOT_FOUND", "message": "User not found" }.
Previous
What is the difference between PUT and PATCH in REST APIs?
Next
What is environment-based configuration in Node.js?