Introduction to Status Codes

HTTP status codes are three-digit numbers that are used to indicate the status of a request or response. They are divided into five categories:
1xx (Informational): These codes indicate that the request has been received and is being processed.
2xx (Successful): These codes indicate that the request has been successfully processed and the response is being sent back to the client.
3xx (Redirection): These codes indicate that the client needs to take additional action to complete the request. For example, the client may be redirected to a different URL.
4xx (Client Error): These codes indicate that there was an error with the request made by the client.
5xx (Server Error): These codes indicate that there was an error on the server while processing the request.
Using Status Codes in Express.js
In Express.js, you can set the status code of a response using the
res.status()method. For example, to set the status code to200 OK, you can use the following code:
app.get('/', (req, res) => {
res.status(200).send('OK');
});
You can also set the status code and send a response in a single method call using res.sendStatus(). For example
app.get('/', (req, res) => {
res.sendStatus(200);
});
Common Status Codes
Here are some common status codes that you may encounter while developing web applications with Express.js:
200 OK
This is the most common status code and indicates that the request has been successfully processed and the response is being sent back to the client.
app.get('/', (req, res) => {
res.sendStatus(200);
});
301 Moved Permanently
This status code indicates that the requested resource has been permanently moved to a new URL. The client should use the new URL to access the resource in the future.
app.get('/old-url', (req, res) => {
res.redirect(301, '/new-url');
});
400 Bad Request
This status code indicates that the request made by the client is invalid. This could be due to a malformed request or missing required parameters.
app.post('/', (req, res) => {
if (!req.body.username) {
res.sendStatus(400);
}
});
401 Unauthorized
This status code indicates that the client is not authorized to access the requested resource. The client may need to authenticate itself to get the requested response.
app.get('/secret', (req, res) => {
if (!req.headers.authorization) {
404 Not Found
This status code indicates that the requested resource could not be found on the server.
app.get('/404', (req, res) => {
res.sendStatus(404);
});
500 Internal Server Error
This status code indicates that there was an error on the server while processing the request.
app.get('/', (req, res) => {
try {
// Some code that may throw an error
} catch (error) {
res.sendStatus(500);
}
});
503 Service Unavailable
This status code indicates that the server is temporarily unable to handle the request due to maintenance or overload. The client may retry the request at a later time
app.get('/', (req, res) => {
if (maintenanceMode) {
res.sendStatus(503);
}
});
Conclusion
In this blog, we looked at different status codes in Express.js and how to use them in your applications. Understanding and using the appropriate status codes can help communicate the status of a request or response and make your applications more user-friendly and reliable.

