View as Json

HTTP Status Codes

The HTTP Status Codes component provides a centralized and type-safe way to reference HTTP status codes across your backend.


Installation Guide

Install the component using the servercn CLI:

npx servercn-cli 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(...);

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];

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.


Status Code Reference

These codes indicate that the client's request was successfully received, understood, and accepted.

Status CodeConstantDescriptionRecommended Usage
200OKThe request was successful and the response contains the requested data.Use for successful GET requests and PUT/ PATCH/ DELETE requests that return a response body.
201CREATEDA new resource was successfully created.Use after a successful POST request that creates a resource. Include the new resource or its identifier in the response.
202ACCEPTEDThe request has been accepted for asynchronous processing.Use when the request is queued or handled by a background job and processing is not yet complete.
204NO_CONTENTThe request was successful but there is no response body.Use for successful DELETE operations or updates where no data needs to be returned.

These codes indicate that the client must take additional action to complete the request.

Status CodeConstantDescriptionRecommended Usage
301MOVED_PERMANENTLYThe resource has been permanently moved to a new URL.Use when an endpoint has been permanently replaced. Clients should update stored URLs.
302FOUNDThe resource is temporarily available at a different URL.Use for temporary redirects such as authentication or OAuth flows.
304NOT_MODIFIEDThe resource has not changed since the last request.Use for caching mechanisms with ETag or Last-Modified headers to reduce bandwidth usage.

These codes indicate that the request contains bad syntax or cannot be fulfilled.

Status CodeConstantDescriptionRecommended Usage
400BAD_REQUESTThe request is malformed or contains invalid parameters.Use for generic validation errors or malformed request payloads.
401UNAUTHORIZEDAuthentication is required or the provided credentials are invalid.Use when access tokens are missing, expired, or invalid.
403FORBIDDENThe client is authenticated but lacks sufficient permissions.Use for authorization failures (e.g., RBAC or ownership checks).
404NOT_FOUNDThe requested resource does not exist.Use when a resource ID, endpoint, or entity cannot be found.
409CONFLICTThe request conflicts with the current state of the resource.Use for duplicate resources (e.g., email already exists) or state conflicts.
422UNPROCESSABLE_ENTITYThe request is valid but fails domain-specific validation rules.Use for semantic validation errors (e.g., weak password, invalid state transition).
429TOO_MANY_REQUESTSToo many requests have been sent in a given time window.Use when rate limits are exceeded. Include retry headers when possible.

These codes indicate the server failed to fulfill an apparently valid request.

Status CodeConstantDescriptionRecommended Usage
500INTERNAL_SERVER_ERRORAn unexpected server error occurred.Use as a fallback for unhandled exceptions or unknown failures.
501NOT_IMPLEMENTEDThe requested functionality is not supported.Use for unimplemented endpoints or feature placeholders.
502BAD_GATEWAYAn invalid response was received from an upstream service.Use when a dependent service returns an invalid response.
503SERVICE_UNAVAILABLEThe service is temporarily unavailable.Use during maintenance windows or when the system is overloaded.
504GATEWAY_TIMEOUTAn upstream service failed to respond in time.Use when timeouts occur while waiting for dependent services.

File & Folder Structure

Loading files...

Installation

npx servercn-cli add http-status-codes