http-errors
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
depd | 2.0.0 | 8.18 kB | MIT | prod | |
inherits | 2.0.4 | 1.98 kB | ISC | prod | |
setprototypeof | 1.2.0 | 1.92 kB | ISC | prod | |
statuses | 2.0.1 | 4.57 kB | MIT | prod | |
toidentifier | 1.0.1 | 2.29 kB | MIT | prod |
The http-errors package is a valuable tool for creating HTTP errors with ease in applications that use Express, Koa, Connect, and similar frameworks. Conveniently accessible through the npm registry, this package works to streamline the creation of comprehensive error objects for HTTP transactions.
To utilize the http-errors package in your Node.js applications, first install the package using the command npm install http-errors
. Once installed, you can require it in your application file. Here's an simple usage example:
var createError = require('http-errors');
var express = require('express');
var app = express();
app.use(function (req, res, next) {
if (!req.user) return next(createError(401, 'Please login to view this page.'));
next();
});
In the example above, if the user object is not available in the request, the middleware creates an HTTP 401 error using http-errors and passes it to the next middleware or the error handling middleware.
Moreover, with the http-errors package, you can either use status codes directly - as in createError(404, 'This video does not exist!')
- or the constructor names, using camel case (i.e., new createError.NotFound()
).
Comprehensive documentation for http-errors is provided directly in the README file in its GitHub repository at https://github.com/jshttp/http-errors
. The document stipulates API details, usage examples, and an exhaustive list of status codes with their corresponding error constructor names in camel case. Whether you're looking to implement particular status codes or discern specific error properties, this documentation is a must-consult resource.