What is http-status?
The http-status npm package provides a collection of HTTP status codes and their associated messages. It is useful for setting and interpreting HTTP response statuses in web applications.
What are http-status's main functionalities?
Accessing Status Codes
You can easily access standard HTTP status codes using the package. This is useful for setting response statuses in your web server.
const httpStatus = require('http-status');
console.log(httpStatus.OK); // 200
console.log(httpStatus.NOT_FOUND); // 404
Accessing Status Messages
The package allows you to retrieve the standard message associated with a given status code. This can be useful for logging or displaying human-readable status messages.
const httpStatus = require('http-status');
console.log(httpStatus['200']); // 'OK'
console.log(httpStatus['404']); // 'Not Found'
Custom Status Codes
You can add custom status codes and messages to the http-status object. This is useful if your application uses non-standard status codes.
const httpStatus = require('http-status');
httpStatus['999'] = 'Custom Status';
console.log(httpStatus['999']); // 'Custom Status'
Other packages similar to http-status
statuses
The statuses package provides similar functionality by offering a list of HTTP status codes and their associated messages. It also allows for custom status codes and messages. Compared to http-status, statuses is more lightweight and focuses solely on status codes and messages without additional features.
http-errors
The http-errors package is used to create HTTP error objects with status codes and messages. It provides more advanced error handling capabilities compared to http-status, including the ability to create custom error classes. It is useful for applications that need detailed error handling and reporting.
HTTP Status code for Node
Utility to interact with HTTP status code.
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. Simple.
Additionally, HTTP code names and messages are respectively accessible with the name "{code}_NAME" and "{code}_MESSAGE".
Extra status code are also made available. They are grouped by categories. Specific properties are exported by http-status
under the property extra
followed by the category name. Also, extra code are merge with regular status code 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 space to signal errors with the client's request.nginx
The NGINX web server software expands the 4xx error space to signal issues with the client's request.cloudflare
Cloudflare's reverse proxy service expands the 5xx series of errors space to signal issues with the origin server.
API
This module is very simple. A documentation would be more complicate than reading the original code.
API sample
const status = require('http-status');
console.info(status[500]);
console.info(status.INTERNAL_SERVER_ERROR);
console.info(status['500_CODE']);
console.info(status['500_MESSAGE']);
const status = require('http-status');
console.info(status.extra.nginx.NO_RESPONSE)
const status = require('http-status/lib/nginx');
console.info(status.IM_A_TEAPOT);
console.info(status.NO_RESPONSE)
Express sample
const express = require('express'),
redis = require('redis'),
status = require('http-status');
const app = express.createServer();
app.get('/', function (req, res) {
const client = redis.createClient();
client.ping(function (err, msg) {
if (err) {
return res.send(status.INTERNAL_SERVER_ERROR);
}
res.send(msg, status.OK);
});
});
app.listen(3000);
Contributors
This package is developed by Adaltas.