HTTP status codes — when to use each — Reference
Supplementary examples
2xx — success responses
// 200 — GET /users, GET /users/3, PUT /users/3 (with JSON body)
res.status(200).json({ data: users });
// 201 — POST /users (resource created)
res.status(201).json({ data: newUser });
// 204 — DELETE /users/3 (nothing to return)
res.status(204).end();4xx — client / request problems
// 400 — validation middleware or controller check
return res.status(400).json({ error: "username is required" });
// 404 — service found no row, or notFound middleware
const err = new Error("User not found");
err.status = 404;
throw err;5xx — server failures
// 500 — default in errorHandler when err.status is missing
// Avoid sending 500 for validation (use 400) or missing rows (use 404)
function errorHandler(err, req, res, next) {
const status = err.status || err.statusCode || 500;
res.status(status).json({ error: err.message });
}Verb → status cheat sheet (typical REST API)
GET /resources 200 (list)
GET /resources/:id 200 (found) | 404 (missing)
POST /resources 201 (created)
PUT /resources/:id 200 (updated) | 404 (missing)
DELETE /resources/:id 204 (deleted) | 404 (missing)Common codes (program reference)
| Code | Name | When to use in your API |
|---|---|---|
200 | OK | GET success; PUT success when returning updated resource |
201 | Created | POST created a new resource (include body or Location header) |
204 | No Content | DELETE success; no response body (use .end()) |
400 | Bad Request | Validation failed, missing/invalid fields, bad JSON |
404 | Not Found | Unknown route (notFound) or id not in database |
500 | Internal Server Error | Unhandled exception; default in errorHandler |
Course example
# 2xx Success
200 OK GET list/detail, PUT update (return body)
201 Created POST created a new resource
204 No Content DELETE succeeded (no response body)
# 4xx Client error (caller or request is wrong)
400 Bad Request Validation failed, malformed JSON, missing field
404 Not Found Resource id does not exist; unknown route (notFound)
# 5xx Server error (bug, database, unhandled exception)
500 Internal Unhandled error; errorHandler default
Additional references & examples
- HTTP status codes (MDN)
When to use 200, 201, 204, 400, 404, 500, and related codes.
- HTTP response status codes (MDN)
Full list with definitions for every standard code.
- REST API Tutorial — status codes
REST-oriented summary of common codes.