What is HTTP status codes and which ones are most important in Node.js APIs?
Why Interviewers Ask This
This tests whether you can apply Node.js knowledge to real-world scenarios. Interviewers are looking for clarity of thought and evidence that you've encountered this in production code.
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" }.
Pro Tip
Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Node.js codebase.
Previous
What is the difference between PUT and PATCH in REST APIs?
Next
What is environment-based configuration in Node.js?