Understanding HTTP Status Codes for Web Development
Nonso Bright, a software engineer in Delta State, explains HTTP status codes, their categories, and how to handle them in React and Node.js applications.
Understanding HTTP Status Codes for Web Development
By Nonso Bright (@nonsobright_), a full-stack software engineer in Delta State, Nigeria
HTTP status codes are the backbone of communication between clients and servers in web development. Whether you're building APIs with Node.js or fetching data in a React app, understanding these codes is critical for creating robust, user-friendly applications. In this guide, I, Nonso Bright (@nonsobright_), will break down HTTP status codes, their categories, and practical ways to handle them in modern web development.
đź§ What Are HTTP Status Codes?
HTTP status codes are three-digit numbers returned by a server in response to a client's request. They indicate the outcome of the request—success, failure, or something else. As a software engineer in Delta State, I’ve used these codes extensively in building scalable APIs and frontends with React, Node.js, and the MERN stack.
Status codes are grouped into five categories:
- 1xx (Informational): Request received, processing continues.
- 2xx (Success): Request successfully processed.
- 3xx (Redirection): Further action needed to complete the request.
- 4xx (Client Error): Client-side issue, like invalid input.
- 5xx (Server Error): Server failed to fulfill a valid request.
🔍 Common HTTP Status Codes
Here are the most common status codes you’ll encounter, with examples in a Node.js/Express API and React frontend:
200 OK
The request succeeded. Commonly used for successful GET or POST requests.
Example (Node.js/Express):
app.get('/api/users', async (req, res) => {
const users = await getUsers();
res.status(200).json(users);
});
Example (React):
import { useEffect, useState } from 'react';
function UserList() {
const [users, setUsers] = useState([]);
useEffect(() => {
fetch('/api/users')
.then(res => {
if (res.status === 200) return res.json();
throw new Error('Failed to fetch users');
})
.then(data => setUsers(data))
.catch(err => console.error(err));
}, []);
return <ul>{users.map(user => <li key={user.id}>{user.name}</li>)}</ul>;
}
201 Created
A resource was successfully created, often after a POST request.
Example (Node.js):
app.post('/api/users', async (req, res) => {
const newUser = await createUser(req.body);
res.status(201).json(newUser);
});
400 Bad Request
The server couldn’t process the request due to client error (e.g., invalid data).
Example (Node.js):
app.post('/api/users', async (req, res) => {
if (!req.body.email) {
return res.status(400).json({ error: 'Email is required' });
}
const user = await createUser(req.body);
res.status(201).json(user);
});
404 Not Found
The requested resource doesn’t exist.
Example (React):
function UserProfile({ userId }) {
const [error, setError] = useState(null);
useEffect(() => {
fetch(`/api/users/${userId}`)
.then(res => {
if (res.status === 404) throw new Error('User not found');
return res.json();
})
.catch(err => setError(err.message));
}, [userId]);
if (error) return <div>{error}</div>;
return <div>User Profile</div>;
}
500 Internal Server Error
A generic server-side error. Avoid exposing sensitive details to clients.
Example (Node.js):
app.get('/api/data', async (req, res) => {
try {
const data = await fetchData();
res.status(200).json(data);
} catch (error) {
res.status(500).json({ error: 'Internal server error' });
}
});
đź› Best Practices for Handling Status Codes
As a software engineer in Delta State, I’ve learned these practices to handle status codes effectively:
- Validate Inputs Early: Catch 4xx errors (e.g., 400, 422) on the server to prevent unnecessary processing.
- Use Meaningful Messages: For 4xx errors, return clear error messages (e.g.,
{ error: 'Invalid email format' }). - Graceful Error Handling in React: Use state to display user-friendly messages for 4xx/5xx errors.
- Log Server Errors: Use tools like Sentry to track 5xx errors without exposing details to clients.
- Test Edge Cases: Write tests (e.g., with Jest) to ensure your API handles all status codes correctly.
Example (React Error Boundary):
import { useState } from 'react';
function ErrorBoundary({ children }) {
const [error, setError] = useState(null);
const handleFetch = async (url) => {
try {
const res = await fetch(url);
if (!res.ok) {
setError(`Error ${res.status}: ${res.statusText}`);
return null;
}
return res.json();
} catch (err) {
setError('Network error');
return null;
}
};
return error ? <div>{error}</div> : children({ handleFetch });
}
🚀 Status Codes in Web3 and Blockchain
In Web3 apps (e.g., Solana-based projects), status codes are critical for API interactions with blockchain nodes. For example, when querying a Solana node:
- 200: Successfully fetched transaction data.
- 429: Rate limit exceeded (common with public nodes).
Example (Node.js with Solana):
import { Connection, clusterApiUrl } from '@solana/web3.js';
app.get('/api/solana/balance', async (req, res) => {
try {
const connection = new Connection(clusterApiUrl('mainnet-beta'));
const balance = await connection.getBalance(req.query.publicKey);
res.status(200).json({ balance });
} catch (error) {
res.status(429).json({ error: 'Rate limit exceeded' });
}
});
âś… Conclusion
HTTP status codes are your guide to building reliable APIs and frontends. By understanding their categories, handling them properly in React and Node.js, and applying best practices, you can create robust applications that delight users. As Nonso Bright (@nonsobright_), a software engineer in Delta State, I use these techniques in my MERN and Web3 projects to ensure scalability and performance.
Explore my portfolio at nonsobright.com or connect with me on GitHub, LinkedIn, or X to learn more about my work in Delta State!
Master status codes, master the web.
Related Articles
Mastering React Server Components in 2025
Dive deep into React Server Components (RSC), how they reshape frontend architecture, and how to integrate them into real-world applications with Next.js.
Building Scalable SaaS Applications with Next.js
Learn how to architect and scale modern SaaS platforms using Next.js, TypeScript, and cloud-native patterns for performance, security, and maintainability.