Enumerated string constants for human-readable HTTP reason phrases corresponding to all status codes.
The primary interface for accessing HTTP reason phrase constants as string values.
/**
* Enumerated HTTP reason phrase constants
* Contains human-readable phrases for all standard status codes
*/
enum ReasonPhrases {
// 1xx Informational Responses
CONTINUE = "Continue",
SWITCHING_PROTOCOLS = "Switching Protocols",
PROCESSING = "Processing",
EARLY_HINTS = "Early Hints",
// 2xx Success
OK = "OK",
CREATED = "Created",
ACCEPTED = "Accepted",
NON_AUTHORITATIVE_INFORMATION = "Non Authoritative Information",
NO_CONTENT = "No Content",
RESET_CONTENT = "Reset Content",
PARTIAL_CONTENT = "Partial Content",
MULTI_STATUS = "Multi-Status",
// 3xx Redirection
MULTIPLE_CHOICES = "Multiple Choices",
MOVED_PERMANENTLY = "Moved Permanently",
MOVED_TEMPORARILY = "Moved Temporarily",
SEE_OTHER = "See Other",
NOT_MODIFIED = "Not Modified",
USE_PROXY = "Use Proxy",
TEMPORARY_REDIRECT = "Temporary Redirect",
PERMANENT_REDIRECT = "Permanent Redirect",
// 4xx Client Errors
BAD_REQUEST = "Bad Request",
UNAUTHORIZED = "Unauthorized",
PAYMENT_REQUIRED = "Payment Required",
FORBIDDEN = "Forbidden",
NOT_FOUND = "Not Found",
METHOD_NOT_ALLOWED = "Method Not Allowed",
NOT_ACCEPTABLE = "Not Acceptable",
PROXY_AUTHENTICATION_REQUIRED = "Proxy Authentication Required",
REQUEST_TIMEOUT = "Request Timeout",
CONFLICT = "Conflict",
GONE = "Gone",
LENGTH_REQUIRED = "Length Required",
PRECONDITION_FAILED = "Precondition Failed",
REQUEST_TOO_LONG = "Request Entity Too Large",
REQUEST_URI_TOO_LONG = "Request-URI Too Long",
UNSUPPORTED_MEDIA_TYPE = "Unsupported Media Type",
REQUESTED_RANGE_NOT_SATISFIABLE = "Requested Range Not Satisfiable",
EXPECTATION_FAILED = "Expectation Failed",
IM_A_TEAPOT = "I'm a teapot",
INSUFFICIENT_SPACE_ON_RESOURCE = "Insufficient Space on Resource",
METHOD_FAILURE = "Method Failure",
MISDIRECTED_REQUEST = "Misdirected Request",
UNPROCESSABLE_ENTITY = "Unprocessable Entity",
LOCKED = "Locked",
FAILED_DEPENDENCY = "Failed Dependency",
UPGRADE_REQUIRED = "Upgrade Required",
PRECONDITION_REQUIRED = "Precondition Required",
TOO_MANY_REQUESTS = "Too Many Requests",
REQUEST_HEADER_FIELDS_TOO_LARGE = "Request Header Fields Too Large",
UNAVAILABLE_FOR_LEGAL_REASONS = "Unavailable For Legal Reasons",
// 5xx Server Errors
INTERNAL_SERVER_ERROR = "Internal Server Error",
NOT_IMPLEMENTED = "Not Implemented",
BAD_GATEWAY = "Bad Gateway",
SERVICE_UNAVAILABLE = "Service Unavailable",
GATEWAY_TIMEOUT = "Gateway Timeout",
HTTP_VERSION_NOT_SUPPORTED = "HTTP Version Not Supported",
INSUFFICIENT_STORAGE = "Insufficient Storage",
NETWORK_AUTHENTICATION_REQUIRED = "Network Authentication Required"
}Usage Examples:
import { ReasonPhrases, StatusCodes } from "http-status-codes";
// Express.js error responses with descriptive messages
app.get("/api/users/:id", (req, res) => {
const user = findUser(req.params.id);
if (!user) {
return res.status(StatusCodes.NOT_FOUND).json({
message: ReasonPhrases.NOT_FOUND,
error: "User not found"
});
}
res.status(StatusCodes.OK).json({
message: ReasonPhrases.OK,
data: user
});
});
// API response standardization
const createApiResponse = (statusCode: number, data?: any) => ({
status: statusCode,
message: getReasonPhrase(statusCode), // Could use ReasonPhrases enum instead
data: data || null,
timestamp: new Date().toISOString()
});
// Logging with human-readable status descriptions
logger.info(`Request completed: ${StatusCodes.OK} ${ReasonPhrases.OK}`);
logger.error(`Request failed: ${StatusCodes.BAD_REQUEST} ${ReasonPhrases.BAD_REQUEST}`);
// Form validation error messages
const validationErrors = {
required: {
status: StatusCodes.BAD_REQUEST,
message: ReasonPhrases.BAD_REQUEST,
details: "Required field missing"
},
unauthorized: {
status: StatusCodes.UNAUTHORIZED,
message: ReasonPhrases.UNAUTHORIZED,
details: "Authentication required"
}
};
// HTTP client response handling
fetch("/api/data")
.then(response => {
if (response.status === StatusCodes.OK) {
console.log(`Success: ${ReasonPhrases.OK}`);
return response.json();
} else if (response.status === StatusCodes.NOT_FOUND) {
throw new Error(`${ReasonPhrases.NOT_FOUND}: Resource not available`);
}
});ReasonPhrases enum constants have a direct 1:1 correspondence with StatusCodes enum constants:
// These pairs always match
StatusCodes.OK === 200
ReasonPhrases.OK === "OK"
StatusCodes.NOT_FOUND === 404
ReasonPhrases.NOT_FOUND === "Not Found"
StatusCodes.INTERNAL_SERVER_ERROR === 500
ReasonPhrases.INTERNAL_SERVER_ERROR === "Internal Server Error"This allows for consistent usage patterns where you can use either the numeric code or human-readable phrase as needed, with both accessing the same underlying HTTP standard.