# @fastify/express 中文文档

规范地址：https://npmcn.com/package/%40fastify/express/

版本：4\.0\.9
npm 元数据更新时间：2026-10-27T08:41:01.996Z
中文译文更新时间：2026-11-14T11:20:42.319Z
软件包许可：MIT

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

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

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

- 包体积（解压后）：90.9 kB，21 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：2 个
- 首次发布：2022-04-27T13:51:47.578Z
- 最新版本发布：2026-10-27T08:41:00.510Z
- 近 30 天下载量：396688（2026-10-15 至 2026-11-13，npm 官方下载统计）
- 已发布版本数：21（近 12 个月 7 个）

## 中文简介

快速化的快取兼容层

## 安装

```sh
npm install @fastify/express
```

也可以使用：`pnpm add @fastify/express`、`yarn add @fastify/express`、`bun add @fastify/express`

## README

# @ fastify/ 表达

[![CI](https://github.com/fastify/fastify-express/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/fastify-express/actions/workflows/ci.yml) [![NPM version](https://img.shields.io/npm/v/@fastify/express.svg?style=flat)](https://www.npmjs.com/package/@fastify/express) [![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)

此插件添加了全部内容 [快递](https://expressjs.com/) 兼容以快取,它会暴露相同的 `use` 它允许您使用任何快取的中间软件或应用程序.  

<table><tbody><tr><td rowspan="2"><b>说明</b></td><td><b>此插件不应作为长期解决方案使用,它旨在帮助您实现从快递到快递的平稳过渡,但您应该随时间推移迁移出快递的具体代码来快递.</b></td></tr><tr><td><b>自兹 <a href="https://github.com/expressjs/express/issues/2761" rel="nofollow noopener noreferrer">Express 不支持 Node.js 核心 HTTP/2 模块</a>,此插件也不支持 HTTP/2 .</b></td></tr></tbody><tbody></tbody></table>

## 安装

```
npm i @fastify/express
```

### 兼容性

| 插件版本 | 快速化版本 |
| --- | --- |
| `>=4.x` | `^5.x` |
| `>=2.x <4.x` | `^4.x` |
| `^1.x` | `^3.x` |
| `^0.x` | `^2.x` |

请注意,如果一个快取版本没有支持,那么该插件的相应版本也一样 上表列出. 见 [快速化 LTS 政策](https://github.com/fastify/fastify/blob/main/docs/Reference/LTS.md) 更多细节.

## 使用量

注册插件并开始使用您的快递中间软件 .

```js
const Fastify = require('fastify')

async function build () {
  const fastify = Fastify()
  await fastify.register(require('@fastify/express'))
  // do you know we also have cors support?
  // https://github.com/fastify/fastify-cors
  fastify.use(require('cors')())
  // express.Application is also accessible
  fastify.express.disabled('x-powered-by') // true
  return fastify
}

build()
  .then(fastify => fastify.listen({ port: 3000 }))
  .catch(console.log)
```

### 添加完整的应用程序

您可以注册整个快递申请,并与快递合作。 记住, `@fastify/express` 刚刚 `express` 盖子下需要同样的身体解析器 `express`。 。 。 .

```js
// index.js
const fastify = require('fastify')()
const express = require('express')
const router = express.Router()

router.use(function (req, res, next) {
  res.setHeader('x-custom', true)
  next()
})

router.get('/hello', (req, res) => {
  res.status(201)
  res.json({ hello: 'world' })
})

router.get('/foo', (req, res) => {
  res.status(400)
  res.json({ foo: 'bar' })
})

router.patch('/bar', (req, res) => {
  if (!req.body || Object.keys(req.body).length === 0) {
    res.status(400)
    res.json({ msg: 'no req.body'})
  } else {
    res.status(200)
    res.json(req.body)
  }
})

router.use('*', (req, res) => {
  res.status(404)
  res.json({ msg: 'not found'})
})

fastify.register(require('@fastify/express'))
  .after(() => {
    fastify.use(express.urlencoded({extended: false})) // for Postman x-www-form-urlencoded
    fastify.use(express.json())

    fastify.use(router)
  })

fastify.listen({ port: 3000 }, console.log)
```

#### 测试您的应用程序

运行 `node index.js` 启动您的服务器。 然后运行以下命令以确保您的服务器正常工作 。 使用可选 `-v` 为动词输出而用卷起的旗帜.

```bash
me@computer ~ % curl -X GET http://localhost:3000/hello
{"hello":"world"}%
me@computer ~ % curl -X GET http://localhost:3000/foo
{"foo":"bar"}%
me@computer ~ % curl -X GET http://localhost:3000/bar
{"msg":"not found"}%
me@computer ~ % curl -X PATCH -H 'content-type:application/json' http://localhost:3000/bar
{"msg":"no req.body"}%
me@computer ~ % curl -X PATCH -H 'content-type:application/json' -d '{"foo2":"bar2"}' http://localhost:3000/bar
{"foo2":"bar2"}%
```

### 封装支持

封装与Fastify一样有效,您可以在子系统中注册插件,您的快取代码将只在里面工作,或者您可以宣布快取插件顶级并注册到嵌入插件中的中件,而中件将只针对特定插件的嵌入路线执行.

_在自己的子系统中注册插件 :_

```js
const fastify = require('fastify')()

fastify.register(subsystem)

async function subsystem (fastify, opts) {
  await fastify.register(require('@fastify/express'))
  fastify.use(require('cors')())
}
```

_在特定插件中注册中间软件 :_

```js
const fastify = require('fastify')()

fastify
  .register(require('@fastify/express'))
  .register(subsystem)

async function subsystem (fastify, opts) {
  fastify.use(require('cors')())
}
```

### 钩子和中间软件

每个已注册的中间软件将在 `onRequest` 钩相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相相. 看看这个 [生命周期](https://fastify.dev/docs/latest/Reference/Lifecycle/) 要更好地了解每个请求是如何执行的.

```js
const fastify = require('fastify')()

fastify
  .register(require('@fastify/express'))
  .register(subsystem)

async function subsystem (fastify, opts) {
  fastify.addHook('onRequest', async (req, reply) => {
    console.log('first')
  })

  fastify.use((req, res, next) => {
    console.log('second')
    next()
  })

  fastify.addHook('onRequest', async (req, reply) => {
    console.log('third')
  })
}
```

### 将中间软件执行限制为特定路径

如果您只需要在特定路径下运行一个中间软件, 只需通过路径作为第一个使用参数即可 !

```js
const fastify = require('fastify')()
const path = require('node:path')
const serveStatic = require('serve-static')

fastify
  .register(require('@fastify/express'))
  .register(subsystem)

async function subsystem (fastify, opts) {
  // Single path
  fastify.use('/css', serveStatic(path.join(__dirname, '/assets')))

  // Wildcard path
  fastify.use('/css/*', serveStatic(path.join(__dirname, '/assets')))

  // Multiple paths
  fastify.use(['/css', '/js'], serveStatic(path.join(__dirname, '/assets')))
}
```

> **说明:** 在前缀插件中, `@fastify/express` 重写字符串、阵列和 RegExp 挂载路径包含前缀 。 RegExp 路径 `^` 内有锚 团体(例如, `/(?:^\/a|^\/b)/`)不正确重写。 使用数组路径 相反: `fastify.use(['/a', '/b'], middleware)`。 。 。 .

### 在代理服务器中环绕快取

可以通过通过将 Express 请求对象包入代理服务器 `createProxyHandler` 函数生成代理处理器。 该函数将接收快取请求对象作为第一个参数.

例如,利用代理机将快取请求中的内容暴露出快取请求.

```js
fastify.decorateRequest('welcomeMessage', 'Hello World');
fastify.register(expressPlugin, {
  createProxyHandler: fastifyRequest => ({
    get (target, prop) {
      if (prop === 'welcomeMessage') {
        return fastifyRequest[prop]
      }

      return target[prop]
    }
  })
})
```

## 类型脚本支持

要使用带有 TypeScript 的模块, 请确保安装 `@types/express`。 。 。 .

您需要添加 `"types": ["@fastify/express"]` 到您的 tsconfig.json 文件使用时 `require` 以导入插件.

## 中间软件替代品

Fastify为最常用的中间软件提供了一些替代品:

| 表达式中件 | 快捷插件 |
| --- | --- |
| [`helmet`](https://github.com/helmetjs/helmet) | [`@fastify/helmet`](https://github.com/fastify/fastify-helmet) |
| [`cors`](https://github.com/expressjs/cors) | [`@fastify/cors`](https://github.com/fastify/fastify-cors) |
| [`serve-static`](https://github.com/expressjs/serve-static) | [`@fastify/static`](https://github.com/fastify/fastify-static) |

## 解决问题

### POST 请求与身体挂起

[车体参数](https://github.com/expressjs/body-parser) 库与 `fastify-express`,当你有 `fastify` 路线和任何 `express` 中间软件. 任何 POST 请求 **机构**,哪个 `body-parser` 会尝试分析,会挂断.

示例应用程序 :

```js
const Fastify = require('fastify')
const Express = require('express')
const expressPlugin = require('@fastify/express')
const bodyParser = require('body-parser')

const fastify = Fastify()
const express = Express()

express.use(bodyParser.urlencoded({ extended: false }))

await fastify.register(expressPlugin)

fastify.use(express)

// this route will never reply
fastify.post('/hello', (req, reply) => {
  return { hello: 'world' }
})
```

对于这个案子,你需要删除 `body-parser`,安装 `@fastify/formbody` 更改 `@fastify/express` 选项:

```js
const Fastify = require('fastify')
const Express = require('express')
const expressPlugin = require('@fastify/express')
const fastifyFormBody = require('@fastify/formbody')

const fastify = Fastify()
const express = Express()

await fastify.register(fastifyFormBody)
await fastify.register(expressPlugin, {
  // run express after `@fastify/formbody` logic
  expressHook: 'preHandler'
})

fastify.use(express)

// it works!
fastify.post('/hello', (req, reply) => {
  return { hello: 'world' }
})
```

## 许可证

许可证根据 [麻省理工学院](https://npmcn.com/package/%40fastify/express/LICENSE)。 。 。 .  
[`express` 许可证](https://github.com/expressjs/express/blob/master/LICENSE)

## 常见问题

### @fastify/express 是什么？

快速化的快取兼容层

### 如何安装 @fastify/express？

在项目目录运行 npm install @fastify/express，或使用 pnpm add / yarn add / bun add。

### @fastify/express 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @fastify/express 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @fastify/express 可以商用吗？

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

### @fastify/express 的下载量有多少？

根据 npm 官方下载统计，@fastify/express 在 2026/10/15 – 2026/11/13 这 30 天内被下载 396,688 次，日均约 13,223 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @fastify/express 还在维护吗？

最新版本 4\.0\.9 发布于 2026/10/27（18 天前）；至今共发布 21 个版本，其中过去 12 个月发布了 7 个；首次发布于 2022/04/27。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

