http-status vs http-status-code vs http-status-codes vs statuses
HTTP Status Code Libraries
http-statushttp-status-codehttp-status-codesstatuses

HTTP Status Code Libraries

These npm packages provide a standardized way to handle HTTP status codes in web applications. They allow developers to easily access and utilize the various HTTP status codes defined by the HTTP/1.1 specification, enhancing code readability and maintainability. Each package has its own unique features and use cases, catering to different developer preferences and project requirements.

Npm Package Weekly Downloads Trend

3 Years

Github Stars Ranking

Stat Detail

Package
Downloads
Stars
Size
Issues
Publish
License
http-status0485338 kB5a year agoBSD-3-Clause
http-status-code07-0-MIT
http-status-codes01,121223 kB323 years agoMIT
statuses028512.5 kB8a year agoMIT

Feature Comparison: http-status vs http-status-code vs http-status-codes vs statuses

Simplicity

  • http-status:

    This package is designed for simplicity, providing a straightforward mapping of HTTP status codes without any additional features. It is easy to use and integrates well into any project without adding unnecessary complexity.

  • http-status-code:

    This package offers a simple interface to access status codes and their descriptions, making it easy for developers to understand and implement HTTP responses without dealing with complex structures.

  • http-status-codes:

    This package provides a simple API for accessing both status codes and their messages, making it easy to implement in applications that require clear communication of HTTP responses.

  • statuses:

    The 'statuses' package is also simple to use, providing a clean interface to access status codes and their descriptions, making it a good choice for developers looking for straightforward functionality.

Detailed Descriptions

  • http-status:

    While it provides the status codes, it does not include detailed descriptions or messages, which may require developers to refer to external documentation for context.

  • http-status-code:

    This package includes detailed descriptions for each status code, making it easier for developers to understand the implications of each code without needing to look them up separately.

  • http-status-codes:

    It provides both status codes and their corresponding messages, allowing developers to easily convey the meaning of each status code in their applications.

  • statuses:

    The 'statuses' package offers a complete mapping of status codes to their textual descriptions, making it ideal for applications that need to communicate status clearly.

Maintenance and Updates

  • http-status:

    This package is lightweight and maintained, but may not receive frequent updates due to its simplicity and limited scope.

  • http-status-code:

    It is actively maintained, ensuring that it stays up-to-date with any changes in HTTP standards and best practices.

  • http-status-codes:

    This package is well-maintained and regularly updated, providing developers with the latest status codes and messages as they evolve.

  • statuses:

    The 'statuses' package is also actively maintained, ensuring that developers have access to the most current and relevant status codes.

Localization Support

  • http-status:

    This package does not provide built-in localization support, which may require additional work for applications that need to present status codes in multiple languages.

  • http-status-code:

    It lacks localization features, so developers will need to implement their own solutions for translating status descriptions if required.

  • http-status-codes:

    This package does not include localization support, which may limit its use in international applications without additional customization.

  • statuses:

    The 'statuses' package does not inherently support localization, but its clear mapping of codes and descriptions can be adapted for localization with additional effort.

Community and Ecosystem

  • http-status:

    This package has a smaller community compared to others, which may limit the availability of resources and support.

  • http-status-code:

    It has a growing community, providing some resources and support for developers looking to implement it in their projects.

  • http-status-codes:

    This package has a robust community and ecosystem, offering plenty of resources, examples, and support for developers.

  • statuses:

    The 'statuses' package benefits from a strong community and is widely used, providing ample resources and support for developers.

How to Choose: http-status vs http-status-code vs http-status-codes vs statuses

  • http-status:

    Choose 'http-status' if you need a simple, straightforward way to access HTTP status codes with minimal overhead. It is lightweight and focuses solely on providing status codes without additional features.

  • http-status-code:

    Opt for 'http-status-code' if you prefer a package that explicitly maps status codes to their descriptions. This can be useful for logging or debugging purposes, as it provides a clear understanding of what each code represents.

  • http-status-codes:

    Select 'http-status-codes' if you want a comprehensive library that includes not only the status codes but also their respective messages. This package is ideal for applications that require detailed status reporting and better context for each code.

  • statuses:

    Use 'statuses' if you need a well-maintained package that provides a complete mapping of status codes to their textual descriptions. It is particularly useful for applications that require localization or custom responses based on status codes.

README for http-status

HTTP Status codes for Node.js

Utility to interact with HTTP status codes.

Migration to v2.x

Version 2 is a migration of the library to ESM modules and TypeScript. The API remains the same. The build system generates both ESM and CommonJS exports.

For ESM users, the import remains the same.

import status from "http-status";
// Or
import { status } from "http-status";

For CommonJs users, update the require statement.

const { status } = require("http-status");
// Or
const { default: status } = require("http-status");

Usage

Once you import or require this module, you may call it with either an HTTP code or a status name. With an HTTP code, you will get the status name while with a status name you will get an HTTP code or some complementary information.

For example, status[418] return IM_A_TEAPOT while status.IM_A_TEAPOT return "I'm a teapot" and status.IM_A_TEAPOT_CODE returns 418.

The package is written in TypeScript and built for CommonJS and ESM.

HTTP Status codes

HTTP code names, information, and classes are respectively accessible with the property {code}_NAME, {code}_MESSAGE and {code}_CLASS. This includes all statuses in the IANA HTTP Status Code Registry, with the only addition being 418 I'm a teapot.

Extra codes

Extra status code are also made available that are not defined in the IANA registry, but used by popular softwares. They are grouped by category. Specific properties are exported by http-status under the property extra followed by the category name. Also, extra codes are merge with regular status codes and made available as modules available inside http-status/lib/{category}.

Available categories are:

unofficial
This represent a list of codes which are not specified by any standard.
iis
Microsoft's Internet Information Services (IIS) web server expands the 4xx error class to signal errors with the client's request.
nginx
The NGINX web server software expands the 4xx error class to signal issues with the client's request.
cloudflare
Cloudflare's reverse proxy service expands the 5xx error class to signal issues with the origin server.

They are accessible throught the status.extra[category] property. It is also possible to import one of the category with import status from "http-status/<category>" or const status = require("http-status/")`. In the later case, all the categories properties are merge with the common HTTP statuses.

HTTP Status code classes

In addition to HTTP status codes, this module also contains status code classes under the classes property. Similar to HTTP codes, you can access class names and messages with the property {class}_NAME and {class}_MESSAGE.

API organization

The API is structured as follows:

100
100_NAME
100_MESSAGE
100_CLASS
CONTINUE
101
101_NAME
101_MESSAGE
101_CLASS
SWITCHING_PROTOCOLS
…
classes.
├── 1xx
├── 1xx_NAME
├── 1xx_MESSAGE
├── INFORMATIONAL
├── 2xx
├── 2xx_NAME
├── 2xx_MESSAGE
├── SUCCESSFUL
├── …
extra.
├── unofficial.
│   ├── 103
│   ├── 103_NAME
│   ├── 103_MESSAGE
│   ├── 103_CLASS
│   ├── CHECKPOINT
│   ├── …
├── iis.
│   ├── 440
│   ├── 440_NAME
│   ├── 440_MESSAGE
│   ├── 440_CLASS
│   ├── LOGIN_TIME_OUT
│   ├── …
├── nginx.
│   ├── 444
│   ├── 444_NAME
│   ├── 444_MESSAGE
│   ├── 444_CLASS
│   ├── NO_RESPONSE
│   ├── …
├── cloudflare.
│   ├── 520
│   ├── 520_NAME
│   ├── 520_MESSAGE
│   ├── 520_CLASS
│   ├── UNKNOWN_ERROR
│   ├── …

For additional information, please refer to original code.

Example API usage

The api example illustrate how to access status names by code and number and how to extra various associated informations.

import status from "http-status";

console.info(status.INTERNAL_SERVER_ERROR);
// Output: 500

console.info(status[500]);
console.info(status[status.INTERNAL_SERVER_ERROR]);
// Both output: "Internal Server Error"

console.info(status["500_NAME"]);
console.info(status[`${status.INTERNAL_SERVER_ERROR}_NAME`]);
// Both output: "INTERNAL_SERVER_ERROR"

console.info(status["500_MESSAGE"]);
console.info(status[`${status.INTERNAL_SERVER_ERROR}_MESSAGE`]);
// Both output: "A generic error message, given when an unexpected condition was encountered and no more specific message is suitable."

console.info(status["500_CLASS"]);
console.info(status[`${status.INTERNAL_SERVER_ERROR}_CLASS`]);
// Both output: "5xx"

Example using classes

import status from "http-status";

const responseCode = status.INTERNAL_SERVER_ERROR;

switch (status[`${responseCode}_CLASS`]) {
  case status.classes.INFORMATIONAL:
    // The responseCode is 1xx
    break;
  case status.classes.SUCCESSFUL:
    // The responseCode is 2xx
    break;
  case status.classes.REDIRECTION:
    // The responseCode is 3xx
    break;
  case status.classes.CLIENT_ERROR:
    // The responseCode is 4xx
    break;
  case status.classes.SERVER_ERROR:
    // The responseCode is 5xx
    break;

  default:
    // Unknown
    break;
}

Example using the extra property

// Accessing property from the NGINX category
import status from "http-status";
console.info(status.extra.nginx.NO_RESPONSE);

// Accessing default HTTP status merged with NGINX status
import status from "http-status/lib/nginx";
console.info(status.IM_A_TEAPOT);
console.info(status.NO_RESPONSE);

Example integrating Express

The express example integrate the library with a real wold usage.

import express from "express";
import redis from "redis";
import status from "http-status";

// New Express HTTP server
const app = express.createServer();
// Regster a route
app.get("/", (req, res) => {
  const client = redis.createClient();
  client.ping((err, msg) => {
    if (err) {
      return res.send(status.INTERNAL_SERVER_ERROR);
    }
    res.send(msg, status.OK);
  });
});
// Start the HTTP server
app.listen(3000);

Contributors

The project is sponsored by Adaltas based in Paris, France. Adaltas offers support and consulting on distributed systems, big data and open source.

Developers

To automatically generate a new version:

npm run release

Package publication is handled by the CI/CD with GitHub action.