HTTP Status Codes
The HTTP Status Codes component provides a centralized and type-safe way to reference HTTP status codes across your backend.
Rather than relying on hard-coded numeric values such as 200, 404, or 500, this component exposes a consistent, strongly typed API that improves clarity and reduces errors across the codebase.
Key benefits include:
- A single source of truth for HTTP status codes
- Improved readability and self-documenting responses
- Strong TypeScript typing and autocomplete
- Reduced risk of incorrect or inconsistent status usage
It is designed to work seamlessly with:
- ApiResponse
- ApiError
- AsyncHandler
- Express middleware
Installation Guide
Install the component using the Servercn CLI:
npx servercn-cli add http-status-codesWhy This Component Exists
In many backend codebases, HTTP status codes are often scattered and hard-coded, making the application harder to maintain and reason about:
As an application grows, this approach leads to:
- Inconsistent status code usage
- Reduced readability
- Difficulty enforcing conventions
- Increased likelihood of subtle bugs
The HTTP Status Codes component solves this by providing a clear, standardized, and type-safe way to reference status codes everywhere in your application.
Basic Implementation
1. MVC Structure
src/constants/status-codes.ts
2. Feature Structure
src/shared/constants/status-codes.ts
Usage Example
The HTTP Status Codes component solves this by providing a clear, standardized, and type-safe way to reference status codes everywhere in your application.
Status Code Reference
2xx Success
These codes indicate that the client's request was successfully received, understood, and accepted.
3xx Redirection
These codes indicate that the client must take additional action to complete the request.
4xx Client Errors
These codes indicate that the request contains bad syntax or cannot be fulfilled.
5xx Server Errors
These codes indicate the server failed to fulfill an apparently valid request.