# @hattip/adapter-node 中文文档

规范地址：https://npmcn.com/package/%40hattip/adapter-node/

版本：0\.0\.49
npm 元数据更新时间：2024-11-03T17:29:47.887Z
中文译文更新时间：2026-11-17T11:45:41.807Z
软件包许可：MIT

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

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

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

- 包体积（解压后）：19.4 kB，19 个文件
- 模块格式：ES Module
- TypeScript 类型：未声明
- 运行依赖：3 个
- 首次发布：2022-03-10T10:48:54.853Z
- 最新版本发布：2024-11-02T10:59:17.684Z
- 近 30 天下载量：29143（2026-10-18 至 2026-11-16，npm 官方下载统计）
- 已发布版本数：52（近 12 个月 0 个）

## 中文简介

Hattip 的节点适配器

## 安装

```sh
npm install @hattip/adapter-node
```

也可以使用：`pnpm add @hattip/adapter-node`、`yarn add @hattip/adapter-node`、`bun add @hattip/adapter-node`

## README

# `@hattip/adapter-node`

(原始内容存档于2017-10-21). Hattip 适配器为Node.js.

## 独立使用

```js
import { createServer } from "@hattip/adapter-node";
import handler from "./handler.js";

createServer(handler).listen(3000, "localhost", () => {
  console.log("Server listening on http://localhost:3000");
});
```

## 使用节点中间软件

```js
import { createServer } from "node:http";
import sirv from "sirv";
import { createMiddleware } from "@hattip/adapter-node";
import hattipHandler from "./entry-hattip.js";

const sirvMiddleware = sirv("public");
const hattipMiddleware = createMiddleware(hattipHandler);

createServer(
  // Chain middlewares manually
  (req, res) => sirvMiddleware(req, res, () => hattipMiddleware(req, res)),
).listen(3000, () => {
  console.log(`Server listening on http://localhost:3000`);
});
```

## 使用快递

```js
import { createMiddleware } from "@hattip/adapter-node";
import handler from "./index.js";
import express from "express";

const app = express();

const middleware = createMiddleware(handler);
app.use(middleware);

app.listen(3000, "localhost", () => {
  console.log("Server listening on http://localhost:3000");
});
```

## API 密码

```ts
/**
 * Creates a request handler to be passed to http.createServer().
 * It can also be used as a middleware in Express or other
 * Connect-compatible frameworks).
 */
function createMiddleware(
  handler: Handler,
  options?: NodeAdapterOptions,
): NodeMiddleware;

/**
 * Create an HTTP server
 */
function createServer(
  handler: Handler,
  adapterOptions?: NodeAdapterOptions,
  serverOptions?: ServerOptions,
): Server;

/** Adapter options */
interface NodeAdapterOptions {
  /**
   * Set the origin part of the URL to a constant value.
   * It defaults to `process.env.ORIGIN`. If neither is set,
   * the origin is computed from the protocol and hostname.
   * To determine the protocol, `req.protocol` is tried first.
   * If `trustProxy` is set, `X-Forwarded-Proto` header is used.
   * Otherwise, `req.socket.encrypted` is used.
   * To determine the hostname, `X-Forwarded-Host`
   * (if `trustProxy` is set) or `Host` header is used.
   */
  origin?: string;
  /**
   * Whether to trust `X-Forwarded-*` headers. `X-Forwarded-Proto`
   * and `X-Forwarded-Host` are used to determine the origin when
   * `origin` and `process.env.ORIGIN` are not set. `X-Forwarded-For`
   * is used to determine the IP address. The leftmost value is used
   * if multiple values are set. Defaults to true if `process.env.TRUST_PROXY`
   * is set to `1`, otherwise false.
   */
  trustProxy?: boolean;
}
```

## 使用本地语言 `fetch`

此适配器使用 [`node-fetch`](https://github.com/node-fetch/node-fetch) 若为 `fetch` 没有全球数据。 您可以通过导入适配器来禁用此行为 `@hattip/adapter-node/native-fetch`,如果 `fetch` 无法获取.

## `context.passThrough` 行为

通话 `context.passThrough` 在用作中间软件时,将请求传递给下一个处理器。 否则将返回占位符响应 .

## `context.platform`

这个适配器让节点成为本土 `request` (单位:千美元)`IncomingMessage`和 `response` (单位:千美元)`ServerResponse`)可用对象 `context.platform`。 。 。 .

## 常见问题

### @hattip/adapter-node 是什么？

Hattip 的节点适配器

### 如何安装 @hattip/adapter-node？

在项目目录运行 npm install @hattip/adapter-node，或使用 pnpm add / yarn add / bun add。

### @hattip/adapter-node 支持 TypeScript 吗？

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

### @hattip/adapter-node 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @hattip/adapter-node 可以商用吗？

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

### @hattip/adapter-node 的下载量有多少？

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

### @hattip/adapter-node 还在维护吗？

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

