1.7.4 • Published 1 month ago

http-status v1.7.4

Weekly downloads
336,240
License
BSD-3-Clause
Repository
github
Last release
1 month ago

Build Status

HTTP Status codes for Node.js

Utility to interact with HTTP status codes.

Usage

Once you require this module, you may call it with either an HTTP code or a message name. With an HTTP code, you will get the message name while with a message name you will get an HTTP code.

HTTP Status codes

HTTP code names, messages, 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 status code are also made available that are not defined in the IANA registry, but used by popular software. 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:

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

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 usage

const status = require('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

const status = require('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
const status = require('http-status');
console.info(status.extra.nginx.NO_RESPONSE)
// Accessing default HTTP status merged with NGINX status
const status = require('http-status/lib/nginx');
console.info(status.IM_A_TEAPOT);
console.info(status.NO_RESPONSE)

Example integrating Express

const express = require('express'),
      redis   = require('redis'),
      status  = require('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

This package is developed by Adaltas.

Developers

To automatically generate a new version:

yarn run release

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

@cinerino/domainnode-express-mongoose-boilerplatek8-healthcheckspoc-bff-serviceinx-serverre_platform_modelsdatacontainers@instaserv/web-awsmonojs@iberry/iberry-factory@pagsa/custom-authorizerdev_nativeapiexpress-app-base@motionpicture/kwskfs-api-javascript-client@mocoin/api-javascript-client@mocoin/domain@toei-jp/cinerino-api-javascript-client@toei-jp/cinerino-api-nodejs-client@toei-jp/cinerino-domain@motionpicture/kwskfs-api-abstract-client@motionpicture/kwskfs-api-nodejs-clientdua-chat@entamecoin/api-nodejs-client@entamecoin/domain@motionpicture/sskts-api-abstract-client@motionpicture/sskts-domain@motionpicture/ttts-domain@entamecoin/api-javascript-clientmicroservice-schedulershaheryarnodenodejs-toggles-moduleares-nextsprbridge@flick/core@gameleap/gl-helpers@sotah-inc/next-local-devuhc-public-api-nodejs@cloudkite/nextmomenta-backendwebsrvexpmidd@labshare/loopback-openapi-v3pullup-cms-basicapp-nextapp-next-modulehulionjsupsurance-apiesm-payloadsyngrisi@everything-registry/sub-chunk-1871@explodingcamera/nextlina-infratech-clilightning-accountskreteslibflitterlib-oauth-toolinghomectrlhobsonhype-serveriblokz-rest-apihuncwotkoa-git-smart-proxykoa-mongoose-crudkoa-mongo-crudkoa-swapikjkestrel2-serverkoa-raisekoa-speckoxgogreengobhash-backendgoogle-tag-manager-detectionguinan-api-errorsguinan-api-respondergrowth-template-api-typescriptks.toolslambidalackey-cmsledger-pivotlb-error-handleripa-customer-settingsipa-inheritanceiros-commonjatin_nandwanijet-nextjet-apijira-todojoi-swagger-expressloopback-component-kong-registerloopback-error-handlernode-jumpstartnext613node-munode-api-problemnode-collection-jsonnode-express-typeorm-appluddzo-nextmappersmith-recordermicrochassis
1.7.4

1 month ago

1.7.3

5 months ago

1.7.2

5 months ago

1.7.1

5 months ago

1.7.0

7 months ago

1.6.2

1 year ago

1.6.1

1 year ago

1.6.0

1 year ago

1.5.3

2 years ago

1.5.2

2 years ago

1.5.1

2 years ago

1.5.0

3 years ago

1.4.2

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.0

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.2.5

7 years ago

0.2.4

7 years ago

0.2.3

8 years ago

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

9 years ago

0.1.8

10 years ago

0.1.7

10 years ago

0.1.6

10 years ago

0.1.4

10 years ago

0.1.3

10 years ago

0.1.1

12 years ago

0.1.0

13 years ago

0.0.1

13 years ago