@hattip/adapter-node

中文文档
0.0.49发布于 2024/11/02MITES Module近 30 天下载 2.9万

Node.js adapter for Hattip

原始使用文档
文档目录7 节
  1. Standalone usage
  2. Using with Node middleware
  3. Using with Express
  4. API
  5. Using native fetch
  6. context.passThrough behavior
  7. context.platform

@hattip/adapter-node

Hattip adapter for Node.js.

Standalone usage

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

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

Using with Node middleware

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`);
});

Using with Express

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

/**
 * 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;
}

Using native fetch

This adapter uses node-fetch if the fetch global is not available. You can disable this behavior by importing your adapter from @hattip/adapter-node/native-fetch, which will throw an error if fetch is not available.

context.passThrough behavior

Calling context.passThrough will pass the request to the next handler when used as a middleware. Otherwise the placeholder response will be returned.

context.platform

This adapter makes Node's native request (IncomingMessage) and response (ServerResponse) objects available in context.platform.

版本与发布记录

@hattip/adapter-node 共发布 52 个版本,首次发布于 2022/03/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.0.49latest2024/11/022 年前
0.0.482024/09/082 年前
0.0.472024/08/112 年前
0.0.462024/06/192 年前
0.0.452024/03/272 年前
0.0.442024/03/092 年前

其他发布通道:canary → 0.0.35-canary.7。

关于 @hattip/adapter-node 的常见问题

@hattip/adapter-node 是什么?

Node.js adapter for 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 和提交记录判断。