HTTP status codes are three-digit numbers that servers send back to clients (like web browsers) to indicate the result of a request. They are part of the HTTP protocol and help communicate whether a request was successful, if there was an error, or if further action is needed.
why are they important?
HTTP status codes are important because they communicate the outcome of a client’s request to the server in a standardized way. These codes inform both the client (e.g., a web browser, API client, or mobile app) and the server about the result of the request and help in troubleshooting or taking further actions.
Additional reasons they are important are for Automation, SEO, security, user experience etc.
Below are some the HTTP codes, when to use them, where to apply them and some examples of how you can implement them.
- 1xx: Informational Responses These codes indicate that the server has received the request and the client should continue to send the request or that the server is processing the request.
-
100 Continue
Meaning: The server has received the initial part of the request and the client should continue with the rest of the request.
When it happens: Typically used during large uploads to ensure the server is ready.
HTTP/1.1 100 Continue
2.2xx: Successful Responses
These indicate that the client's request was received and processed successfully.
200 OK
- Meaning: The request was successful.
- When it happens: For successful GET, POST, PUT, DELETE requests.
res.status(200).json({ message: "Request was successful" });
201 Created
Meaning: The request was successful and a new resource was created.
When it happens: When a new record or resource (like a user or object) is successfully created.
res.status(201).json({ message: "User created successfully", user: newUser });
204 No Content
Meaning: The request was successful, but there's no content to send back.
When it happens: Often used after a DELETE request when there's no additional information to send.
res.status(204).send(); // No content
- 3xx: Redirection Responses These codes indicate that the client must take some additional action to complete the request.
301 Moved Permanently
Meaning: The resource has been permanently moved to a new URL.
When it happens: For URL redirections, often for old resources moved to new locations.
HTTP/1.1 301 Moved Permanently
Location: https://new-url.com
302 Found (Temporary Redirect)
Meaning: The resource has been temporarily moved to a different URL.
When it happens: Common for temporary URL redirections.
res.redirect(302, 'https://temporary-url.com');
- 4xx: Client Errors These indicate that something is wrong with the request.
400 Bad Request
Meaning: The server could not understand the request due to invalid syntax or bad data.
When it happens: When the client sends malformed request data.
res.status(400).json({ error: "Invalid data format" });
401 Unauthorized
Meaning: Authentication is required or has failed.
When it happens: When the user tries to access a protected resource without proper credentials.
res.status(401).json({ error: "Unauthorized access" });
403 Forbidden
Meaning: The client is authenticated, but does not have permission to access the resource.
When it happens: When a user is logged in but doesn't have the right permissions to perform an action.
res.status(403).json({ error: "Access denied" });
404 Not Found
Meaning: The server cannot find the requested resource.
When it happens: When the user requests a resource that doesn’t exist.
res.status(404).json({ error: "Resource not found" });
405 Method Not Allowed
Meaning: The request method is not supported for the requested resource.
When it happens: When the user tries to use a method (like POST) on a resource that only allows other methods (like GET).
res.status(405).json({ error: "Method not allowed" });
409 Conflict
Meaning: The request conflicts with the current state of the server.
When it happens: Often used for version conflicts, duplicate entries, or when data already exists.
res.status(409).json({ error: "Resource already exists" });
429 Too Many Requests
Meaning: The client has sent too many requests in a given amount of time ("rate limiting").
When it happens: When an API restricts the number of requests a client can make in a given period.
res.status(429).json({ error: "Too many requests, please try again later" });
- 5xx: Server Errors These indicate that the server has encountered a problem while processing the request.
500 Internal Server Error
Meaning: The server encountered a general error that prevented it from fulfilling the request.
When it happens: When something goes wrong on the server side.
res.status(500).json({ error: "Internal Server Error" });
502 Bad Gateway
Meaning: The server received an invalid response from an upstream server.
When it happens: Common when your app is acting as a gateway or proxy and the upstream server fails.
res.status(502).json({ error: "Bad Gateway" });
503 Service Unavailable
Meaning: The server is currently unavailable (overloaded or down for maintenance).
When it happens: When the server is down temporarily or too busy to handle the request.
res.status(503).json({ error: "Service Unavailable" });
504 Gateway Timeout
Meaning: The server was acting as a gateway or proxy and did not receive a timely response from the upstream server.
When it happens: When a request to another server times out.
res.status(504).json({ error: "Gateway Timeout" });
Conclusion
Understanding and handling HTTP status codes properly ensures better user experience and clear communication between the frontend and backend.
Also to make understand that generally:
- 200-299 = success
- 300-399 = it's somewhere else
- 400-499 = the client/user screwed up
- 500-599 = the server screwed up
Example:
app.post('/user', async (req, res) => {
try {
const user = await User.create(req.body);
res.status(201).json({ message: "User created successfully", user });
} catch (error) {
if (error.code === 11000) { // Duplicate key error
res.status(409).json({ error: "User already exists" });
} else {
res.status(500).json({ error: "Internal Server Error" });
}
}
});
Top comments (0)