HTTP Statuses Utility

2.0.2 · active · verified Sun Apr 19

The `statuses` package provides a comprehensive utility for working with HTTP status codes and messages in Node.js environments. It consolidates status information from official sources like the IANA Status Code Registry, as well as common implementations from the Node.js, NGINX, and Apache HTTP Server projects. Currently at stable version 2.0.2, the library maintains a steady release cadence focused on stability, minor updates, and continuous integration improvements rather than frequent feature additions, reflecting its role as a mature, low-level building block. Its key differentiator lies in its comprehensive data source aggregation and direct mapping capabilities, allowing developers to easily convert between numeric codes and descriptive messages, and access properties like whether a status indicates an empty body, a redirect, or a retryable condition. Unlike some HTTP utilities, `statuses` will throw an error for unknown codes or messages, ensuring strict adherence to recognized HTTP semantics.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates core functionality including code-to-message and message-to-code lookups, accessing status code lists and properties like `empty` and `redirect`, and error handling for unknown values.

const status = require('statuses');

// Get message from a numeric status code
console.log('Status 403:', status(403));
console.log('Status "403":', status('403'));

// Get code from a status message (case-insensitive)
console.log('Code for "forbidden":', status('forbidden'));
console.log('Code for "Not Found":', status('Not Found'));

// Access all known status codes
console.log('All status codes (first 5):', status.codes.slice(0, 5));

// Check if a status code expects an empty body
console.log('Does 200 expect empty body?', !!status.empty[200]);
console.log('Does 204 expect empty body?', !!status.empty[204]);

// Check if a status code is a redirect
console.log('Is 200 a redirect?', !!status.redirect[200]);
console.log('Is 301 a redirect?', !!status.redirect[301]);

// Get message directly from the message object
console.log('Message for 404:', status.message[404]);

// Error handling for unknown values
try {
    status(306);
} catch (e) {
    console.error('Error for status(306):', e.message);
}

try {
    status('foo');
} catch (e) {
    console.error('Error for status("foo"):', e.message);
}

view raw JSON →