Utility to interact with HTTP status codes.
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.
importstatusfrom"http-status";// Orimport{status}from"http-status";For CommonJs users, update the require statement.
const{ status }=require("http-status");// Orconst{default: status}=require("http-status");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, the following will return status[418] --> "I'm a teapot"status["418_NAME"] --> "IM_A_TEAPOT"status["418_MESSAGE"] --> "Any attempt to brew coffee with a teapot should result in the error code "418 I'm a teapot". The resulting entity body MAY be short and stout." . status["418_CLASS"] --> "4xx"status.IM_A_TEAPOT --> 418
The package is written in TypeScript and built for CommonJS and ESM.
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 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.
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.
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.
The api example illustrate how to access status names by code and number and how to extra various associated informations.
importstatusfrom"http-status";console.info(status.INTERNAL_SERVER_ERROR);// Output: 500console.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"importstatusfrom"http-status";constresponseCode=status.INTERNAL_SERVER_ERROR;switch(status[`${responseCode}_CLASS`]){casestatus.classes.INFORMATIONAL:
// The responseCode is 1xxbreak;casestatus.classes.SUCCESSFUL:
// The responseCode is 2xxbreak;casestatus.classes.REDIRECTION:
// The responseCode is 3xxbreak;casestatus.classes.CLIENT_ERROR:
// The responseCode is 4xxbreak;casestatus.classes.SERVER_ERROR:
// The responseCode is 5xxbreak;default:
// Unknownbreak;}// Accessing property from the NGINX categoryimportstatusfrom"http-status";console.info(status.extra.nginx.NO_RESPONSE);// Accessing default HTTP status merged with NGINX statusimportstatusfrom"http-status/lib/nginx";console.info(status.IM_A_TEAPOT);console.info(status.NO_RESPONSE);The express example integrate the library with a real wold usage.
importexpressfrom"express";importredisfrom"redis";importstatusfrom"http-status";// New Express HTTP serverconstapp=express.createServer();// Regster a routeapp.get("/",(req,res)=>{constclient=redis.createClient();client.ping((err,msg)=>{if(err){returnres.send(status.INTERNAL_SERVER_ERROR);}res.send(msg,status.OK);});});// Start the HTTP serverapp.listen(3000);- David Worms: https://github.com/wdavidw
- Daniel Gasienica: https://github.com/gasi
- Rodrigo: rfsbraz@gmail.com
- Paul Vollmer: paul.vollmer@fh-potsdam.de
- James Barcellano: https://github.com/ckeboss
The project is sponsored by Adaltas based in Paris, France. Adaltas offers support and consulting on distributed systems, big data and open source.
To automatically generate a new version:
npm run releasePackage publication is handled by the CI/CD with GitHub action.