# @as-integrations/express4 中文文档

规范地址：https://npmcn.com/package/%40as-integrations/express4/

版本：1\.1\.2
npm 元数据更新时间：2026-05-28T22:47:46.813Z
中文译文更新时间：2026-11-06T21:56:40.154Z
软件包许可：MIT

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

原始 npm 页面：https://www.npmjs.com/package/%40as-integrations/express4
原文对照：https://npmcn.com/package/%40as-integrations/express4/?lang=original
项目主页：https://github.com/apollo-server-integrations/apollo-server-integration-express4
源码仓库：https://github.com/apollo-server-integrations/apollo-server-integration-express4

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

- 包体积（解压后）：148 kB，19 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=20
- 运行依赖：0 个
- peer 依赖：express ^4\.0\.0，@apollo/server ^4\.0\.0 \|\| ^5\.0\.0
- 首次发布：2025-05-08T00:00:11.124Z
- 最新版本发布：2025-07-17T17:47:40.381Z
- 近 30 天下载量：2050995（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：5（近 12 个月 0 个）

## 中文简介

阿波罗服务器集成 Express v4 使用

## 安装

```sh
npm install @as-integrations/express4
```

也可以使用：`pnpm add @as-integrations/express4`、`yarn add @as-integrations/express4`、`bun add @as-integrations/express4`

## README

# Express v4 的阿波罗服务器集成

## **导言**

这个软件包通过导出一个执行GraphQL操作的中间软件,将Aporo Server v4与Express网络框架的第4版集成.

它与出口于 `@apollo/server/express4` 从核心 `@apollo/server` v4 软件包. 它被单独提供,这样我们也可以提供与Express v5的集成(`@as-integrations/express5`;未来阿波罗服务器的主要版本将不会有内置支持Express.

## **所需资源**

-   **[节点.js v20](https://nodejs.org/)** 或稍后时间
-   **[快捷键 v4](https://expressjs.com/)**; 此软件包确实 _没有_ 支持快递 v5
-   **[阿波罗服务器 v4](https://www.apollographql.com/docs/apollo-server/)**

## **安装**

```bash
npm install @apollo/server graphql express@4 @as-integrations/express4 cors
```

## **使用量**

设置 [快递](https://expressjs.com/) 打开 [阿波罗服务器](https://www.apollographql.com/docs/apollo-server/) 就像你通常会,然后通过使用 `expressMiddleware` 中间软件 :

```typescript
import { ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@as-integrations/express4';
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';
import express from 'express';
import http from 'http';
import cors from 'cors';
import { typeDefs, resolvers } from './schema';

interface MyContext {
  token?: string;
}

// Required logic for integrating with Express
const app = express();
// Our httpServer handles incoming requests to our Express app.
// Below, we tell Apollo Server to "drain" this httpServer,
// enabling our servers to shut down gracefully.
const httpServer = http.createServer(app);

const server = new ApolloServer<MyContext>({
  typeDefs,
  resolvers,
  plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
});
// Ensure we wait for our server to start
await server.start();

// Set up our Express middleware to handle CORS, body parsing,
// and our expressMiddleware function.
app.use(
  '/',
  cors<cors.CorsRequest>(),
  express.json(),
  // expressMiddleware accepts the same arguments:
  // an Apollo Server instance and optional configuration options
  expressMiddleware(server, {
    context: async ({ req }) => ({ token: req.headers.token }),
  }),
);

await new Promise<void>((resolve) =>
  httpServer.listen({ port: 4000 }, resolve),
);
console.log(`🚀 Server ready at http://localhost:4000/`);
```

说明: **你必须打电话等待 `server.start()` 在使用整合之前.**

## 常见问题

### @as-integrations/express4 是什么？

阿波罗服务器集成 Express v4 使用

### 如何安装 @as-integrations/express4？

在项目目录运行 npm install @as-integrations/express4，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：express（^4\.0\.0）、@apollo/server（^4\.0\.0 \|\| ^5\.0\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @as-integrations/express4 支持 TypeScript 吗？

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

### @as-integrations/express4 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @as-integrations/express4 需要什么版本的 Node\.js？

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

### @as-integrations/express4 可以商用吗？

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

### @as-integrations/express4 的下载量有多少？

根据 npm 官方下载统计，@as-integrations/express4 在 2026/10/07 – 2026/11/05 这 30 天内被下载 2,050,995 次，日均约 68,367 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @as-integrations/express4 还在维护吗？

最新版本 1\.1\.2 发布于 2025/07/18（1\.3 年前）；至今共发布 5 个版本，其中过去 12 个月发布了 0 个；首次发布于 2025/05/08。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

