# http-errors 中文文档

规范地址：https://npmcn.com/package/http-errors/

版本：2\.0\.1
npm 元数据更新时间：2025-11-20T19:12:22.971Z
中文译文更新时间：2026-11-04T16:52:33.797Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/http-errors
原文对照：https://npmcn.com/package/http-errors/?lang=original
项目主页：https://github.com/jshttp/http-errors#readme
源码仓库：https://github.com/jshttp/http-errors

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：19.1 kB，5 个文件
- TypeScript 类型：未声明
- Node.js 要求：\>= 0\.8
- 运行依赖：5 个
- 首次发布：2012-11-18T05:58:21.693Z
- 最新版本发布：2025-11-20T19:12:22.755Z
- 近 30 天下载量：697495635（2026-10-04 至 2026-11-02，npm 官方下载统计）
- 已发布版本数：30（近 12 个月 1 个）

## 中文简介

创建HTTP错误对象

## 安装

```sh
npm install http-errors
```

也可以使用：`pnpm add http-errors`、`yarn add http-errors`、`bun add http-errors`

## README

# 的http错误

[![NPM Version](https://badgen.net/npm/v/http-errors)](https://npmjs.org/package/http-errors) [![NPM Downloads](https://badgen.net/npm/dm/http-errors)](https://nodejs.org/en/download) [![Node.js Version](https://badgen.net/npm/node/http-errors)](https://nodejs.org/en/download) [![Build Status](https://badgen.net/github/checks/jshttp/http-errors/master?label=ci)](https://github.com/jshttp/http-errors/actions?query=workflow%3Aci) [![Test Coverage](https://badgen.net/coveralls/c/github/jshttp/http-errors/master)](https://coveralls.io/r/jshttp/http-errors?branch=master)

轻松地为Express， Koa， Connect等创建HTTP错误。

## 安装

这是一个 [node . js](https://nodejs.org/en/) 模块可通过 [npm注册表](https://www.npmjs.com/)． 安装是使用 [`npm install` 命令](https://docs.npmjs.com/getting-started/installing-npm-packages-locally)：

```console
$ npm install http-errors
```

## 例子

```js
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

这是当前的API，当前从Koa中提取，可能会发生更改。

### 错误的属性

-   `expose` -可以用来表示如果 `message` 应该发给客户， 违约, `false` 当 `status` > = 500
-   `headers` -可以是头名称到要发送到的值的对象 客户端，默认为 `undefined`。定义时，键名应该全部 是小写
-   `message` -传统的错误信息，应该保持简短 一行
-   `status` -错误的状态码，镜像 `statusCode` 对于一般 兼容性
-   `statusCode` -错误状态码，默认为 `500`

### createError（\[status\], \[message\], \[properties\]）

用给定的消息创建一个新的错误对象 `msg`。 错误对象继承自 `createError.HttpError`。

```js
var err = createError(404, 'This video does not exist!')
```

-   `status: 500` -状态码的数字形式
-   `message` -错误消息，默认为该状态码的节点文本。
-   `properties` -附加到对象的自定义属性

### createError（\[status\], \[error\], \[properties\]）

扩展给定的 `error` 对象与 `createError.HttpError` 属性。这不会改变给定对象的继承 `error` 对象，以及修改后的 `error` 对象是 返回值。

```js
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` -状态码的数字形式
-   `error` -要扩展的错误对象
-   `properties` -附加到对象的自定义属性

### createError.isHttpError (val)

确定所提供的 `val` 是一个 `HttpError`。这将返回 `true` 如果错误继承自 `HttpError` 此模块的构造函数或 匹配此模块创建的错误的“duck类型”。的所有输出 这个 `createError` 工厂将返回 `true` 对于这个函数，包括 如果不是——`HttpError` 被送进了工厂。

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

用给定的消息创建一个新的错误对象 `msg`。 错误对象继承自 `createError.HttpError`。

```js
var err = new createError.NotFound()
```

-   `code` -状态码的数字形式
-   `name` -错误的名称为“颠簸情况”，即 `NotFound` 或 `InternalServerError`。

#### 所有构造函数的列表

| 状态码 | 构造函数的名字 |
| --- | --- |
| 400 | BadRequest |
| 401 | 未经授权的 |
| 402 | PaymentRequired |
| 403 | 禁止 |
| 404 | NotFound |
| 405 | MethodNotAllowed |
| 406 | NotAcceptable |
| 407 | ProxyAuthenticationRequired |
| 408 | RequestTimeout |
| 409 | 冲突 |
| 410 | 走了 |
| 411 | LengthRequired |
| 412 | PreconditionFailed |
| 413 | PayloadTooLarge |
| 414 | URITooLong |
| 415 | UnsupportedMediaType |
| 416 | RangeNotSatisfiable |
| 417 | ExpectationFailed |
| 418 | ImATeapot |
| 421 | MisdirectedRequest |
| 422 | UnprocessableEntity |
| 423 | 锁 |
| 424 | FailedDependency |
| 425 | 做 |
| 426 | UpgradeRequired |
| 428 | PreconditionRequired |
| 429 | TooManyRequests |
| 431 | RequestHeaderFieldsTooLarge |
| 451 | UnavailableForLegalReasons |
| 500 | InternalServerError |
| 501 | NotImplemented |
| 502 | BadGateway |
| 503 | ServiceUnavailable |
| 504 | GatewayTimeout |
| 505 | HTTPVersionNotSupported |
| 506 | VariantAlsoNegotiates |
| 507 | InsufficientStorage |
| 508 | LoopDetected |
| 509 | BandwidthLimitExceeded |
| 510 | NotExtended |
| 511 | NetworkAuthenticationRequired |

## 许可证

[麻省理工学院](https://npmcn.com/package/http-errors/LICENSE)

## 常见问题

### http-errors 是什么？

创建HTTP错误对象

### 如何安装 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 和提交记录判断。

