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 add http-status-codes

Why 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:

res.status(200).json(...);
res.status(401).send(...);
res.status(500).json(...);

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

src/constants/status-codes.ts
export const STATUS_CODES = {
  // 2xx Success
  OK: 200,
  CREATED: 201,
  ACCEPTED: 202,
  NO_CONTENT: 204,
 
  // 3xx Redirection
  MOVED_PERMANENTLY: 301,
  FOUND: 302,
  NOT_MODIFIED: 304,
 
  // 4xx Client Errors
  BAD_REQUEST: 400,
  UNAUTHORIZED: 401,
  FORBIDDEN: 403,
  NOT_FOUND: 404,
  CONFLICT: 409,
  UNPROCESSABLE_ENTITY: 422,
  TOO_MANY_REQUESTS: 429,
 
  // 5xx Server Errors
  INTERNAL_SERVER_ERROR: 500,
  NOT_IMPLEMENTED: 501,
  BAD_GATEWAY: 502,
  SERVICE_UNAVAILABLE: 503,
  GATEWAY_TIMEOUT: 504
} as const;
 
export type StatusCode = (typeof STATUS_CODES)[keyof typeof STATUS_CODES];
src/shared/constants/status-codes.ts
export const STATUS_CODES = {
  // 2xx Success
  OK: 200,
  CREATED: 201,
  ACCEPTED: 202,
  NO_CONTENT: 204,
 
  // 3xx Redirection
  MOVED_PERMANENTLY: 301,
  FOUND: 302,
  NOT_MODIFIED: 304,
 
  // 4xx Client Errors
  BAD_REQUEST: 400,
  UNAUTHORIZED: 401,
  FORBIDDEN: 403,
  NOT_FOUND: 404,
  CONFLICT: 409,
  UNPROCESSABLE_ENTITY: 422,
  TOO_MANY_REQUESTS: 429,
 
  // 5xx Server Errors
  INTERNAL_SERVER_ERROR: 500,
  NOT_IMPLEMENTED: 501,
  BAD_GATEWAY: 502,
  SERVICE_UNAVAILABLE: 503,
  GATEWAY_TIMEOUT: 504
} as const;
 
export type StatusCode = (typeof STATUS_CODES)[keyof typeof STATUS_CODES];

Usage Example

import { STATUS_CODES } from "../utils/status-codes";
 
res.status(STATUS_CODES.OK).json(...);
res.status(STATUS_CODES.BAD_REQUEST).send(...);
res.status(STATUS_CODES.INTERNAL_SERVER_ERROR).json(...);

The HTTP Status Codes component solves this by providing a clear, standardized, and type-safe way to reference status codes everywhere in your application.

File & Folder Structure

Select a file to view its contents

Installation

npx servercn add http-status-codes