http-errors

中文文档
2.0.1发布于 2025/11/21MIT近 30 天下载 7亿

Create HTTP error objects

原始使用文档
文档目录9 节
  1. Install
  2. Example
  3. API
  4. Error Properties
  5. createError([status], [message], [properties])
  6. createError([status], [error], [properties])
  7. createError.isHttpError(val)
  8. new createError[code || name]([msg]))
  9. License

http-errors

NPM Version NPM Downloads Node.js Version Build Status Test Coverage

Create HTTP errors for Express, Koa, Connect, etc. with ease.

Install

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install http-errors

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()
})

API

This is the current API, currently extracted from Koa and subject to change.

Error Properties

  • expose - can be used to signal if message should be sent to the client, defaulting to false when status >= 500
  • headers - can be an object of header names to values to be sent to the client, defaulting to undefined. When defined, the key names should all be lower-cased
  • message - the traditional error message, which should be kept short and all single line
  • status - the status code of the error, mirroring statusCode for general compatibility
  • statusCode - the status code of the error, defaulting to 500

createError([status], [message], [properties])

Create a new error object with the given message msg. The error object inherits from createError.HttpError.

var err = createError(404, 'This video does not exist!')
  • status: 500 - the status code as a number
  • message - the message of the error, defaulting to node's text for that status code.
  • properties - custom properties to attach to the object

createError([status], [error], [properties])

Extend the given error object with createError.HttpError properties. This will not alter the inheritance of the given error object, and the modified error object is the return value.

fs.readFile('foo.txt', function (err, buf) {
  if (err) {
    if (err.code === 'ENOENT') {
      var httpError = createError(404, err, { expose: false })
    } else {
      var httpError = createError(500, err)
    }
  }
})
  • status - the status code as a number
  • error - the error object to extend
  • properties - custom properties to attach to the object

createError.isHttpError(val)

Determine if the provided val is an HttpError. This will return true if the error inherits from the HttpError constructor of this module or matches the "duck type" for an error this module creates. All outputs from the createError factory will return true for this function, including if an non-HttpError was passed into the factory.

new createError[code || name]([msg]))

Create a new error object with the given message msg. The error object inherits from createError.HttpError.

var err = new createError.NotFound()
  • code - the status code as a number
  • name - the name of the error as a "bumpy case", i.e. NotFound or InternalServerError.

List of all constructors

Status CodeConstructor Name
400BadRequest
401Unauthorized
402PaymentRequired
403Forbidden
404NotFound
405MethodNotAllowed
406NotAcceptable
407ProxyAuthenticationRequired
408RequestTimeout
409Conflict
410Gone
411LengthRequired
412PreconditionFailed
413PayloadTooLarge
414URITooLong
415UnsupportedMediaType
416RangeNotSatisfiable
417ExpectationFailed
418ImATeapot
421MisdirectedRequest
422UnprocessableEntity
423Locked
424FailedDependency
425TooEarly
426UpgradeRequired
428PreconditionRequired
429TooManyRequests
431RequestHeaderFieldsTooLarge
451UnavailableForLegalReasons
500InternalServerError
501NotImplemented
502BadGateway
503ServiceUnavailable
504GatewayTimeout
505HTTPVersionNotSupported
506VariantAlsoNegotiates
507InsufficientStorage
508LoopDetected
509BandwidthLimitExceeded
510NotExtended
511NetworkAuthenticationRequired

License

MIT

版本与发布记录

http-errors 共发布 30 个版本,首次发布于 2012/11/18;过去 12 个月发布了 1 个版本。

版本发布日期距今
2.0.1latest2025/11/2111 个月前
2.0.02021/12/184 年前
1.8.12021/11/154 年前
1.8.02020/06/296 年前
1.7.32019/06/257 年前
1.7.22019/02/197 年前

关于 http-errors 的常见问题

http-errors 是什么?

Create HTTP error objects

如何安装 http-errors?

在项目目录运行 npm install http-errors,或使用 pnpm add / yarn add / bun add。

http-errors 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/http-errors 是否存在。

http-errors 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 0.8。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

http-errors 可以商用吗?

http-errors 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

http-errors 的下载量有多少?

根据 npm 官方下载统计,http-errors 在 2026/10/04 – 2026/11/02 这 30 天内被下载 697,495,635 次,日均约 23,249,855 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

http-errors 还在维护吗?

最新版本 2.0.1 发布于 2025/11/21(11 个月前);至今共发布 30 个版本,其中过去 12 个月发布了 1 个;首次发布于 2012/11/18。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。