ITADN
hattipjs/hattip
hattipjs/hattip · 文件 下载 ZIP
文件最后提交记录最后更新时间
README.md
以下内容由 AI 翻译,如有问题请点此提交 issue 反馈

Hattip

(nothing) 类似 Express.js。

关注:Twitter > @cyco130 & Twitter > @brillout
聊天:Discord > CubeshashHattip

为什么选择 Hattip?

不要编写仅适用于 Express.js 的服务器代码,而是编写可部署在任何地方的服务器代码:AWS、Cloudflare Workers、Fastly、Vercel、VPS、...

Hattip 是什么?

Hattip 是一组用于构建 HTTP 服务器应用程序的 JavaScript 包。

  • ✨ 现代化:基于当前和未来的 Web 标准(Fetch API & WinterCG)。
  • 🌍 通用性:可在任何地方运行(Node.js、边缘、Deno、...)。
  • 🧩 模块化:按需使用,多或少皆可。
  • 🪛 极简主义:包含所需的一切,没有多余的。

它旨在构建一个通用中间件生态系统,可在整个 JavaScript 宇宙中使用。

// handler.js

// This request handler works anywhere, e.g. Node.js, Cloudflare Workers, and Fastly.

export default (context) => {
  const { pathname } = new URL(context.request.url);
  if (pathname === "/") {
    return new Response("Hello from Hattip.");
  }
  if (pathname === "/about") {
    return new Response(
      "This HTTP handler works in Node.js, Cloudflare Workers, and Fastly.",
    );
  }
  return new Response("Not found.", { status: 404 });
};

Hattip 处理器会接收一个 context 对象,该对象表示请求上下文,并包含 context.request,它是一个标准的 Request 对象。它返回一个标准的 Response 对象(或一个此类对象的 promise)。ResponseRequest 遵循 Fetch API 标准。因此,如果你熟悉 service workers、Cloudflare Workers 或 Deno,你会感到如鱼得水。如果不熟悉,今天就学习这个明天将无处不在的标准。

我们相信 JavaScript 生态系统将拥有一个多样化但互操作的未来,我们正密切关注 WinterCG,它奠定了 Fetch API 之外的基础。

Adapters

  • ✅ Bun
  • ✅ Cloudflare Workers
  • ✅ Deno(包括 Deno Deploy)
  • ✅ Express.js(在你的 Express.js 应用中使用 Hattip 处理器/中间件)
  • ✅ Fastly
  • ✅ Lagon
  • ✅ Netlify Edge Functions
  • ✅ Netlify Functions
  • ✅ Node.js
  • ✅ uWebSockets.js
  • ✅ Vercel Edge
  • ✅ Vercel Serverless
  • 🚧 Service Workers

Adapters 让你可以在任何平台上运行 Hattip。以下是如何在 Node.js 中使用 Hattip:

// entry-node.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");
});

...以及在 Cloudflare Workers 上:

// entry-cfw.js
import cloudflareWorkersAdapter from "@hattip/adapter-cloudflare-workers";
import handler from "./handler.js";

export default {
  fetch: cloudflareWorkersAdapter(handler),
};

你甚至可以将你的 Hattip 应用程序用作 Express 中间件,当你必须使用那个在其他地方没有替代方案的特定 Express 库时:

// entry-express.js
import { createMiddleware } from "@hattip/adapter-node";
import handler from "./handler.js";
import express from "express";
import oldAndRustyExpressMiddleware from "old-and-rusty-express-middleware";

const hattip = createMiddleware(handler);
const app = express();

// TODO: Replace with shinyNewHatTipMiddleware once ready
app.use(oldAndRustyExpressMiddleware());
app.use(hattip);

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

中间件系统

来自 @hattip/compose 包的 compose 函数可用于将多个处理器组合为一个,从而创建一个简单但功能强大的中间件系统。每个处理器按顺序调用,直到其中一个返回响应。处理器可以通过不返回任何内容或调用 ctx.next() 来将控制权传递给下一个处理器。后者允许处理器在返回之前修改响应:

import { compose } from "@hattip/compose";

// Example of making things available in `ctx`
// Middleware to parse the URL into a URL object
const urlParser = (ctx) => {
  ctx.url = new URL(ctx.request.url);
};

// Example of modifying the response
// Middleware to add an X-Powered-By header
const poweredBy = async (ctx) => {
  const response = await ctx.next();
  response.headers.set("X-Powered-By", "Hattip");
  return response;
};

// Hattip does have a router, this is to illustrate the basics
const homeHandler = (ctx) => {
  if (ctx.url.pathname === "/") {
    return new Response("Home");
  }
};

const fooHandler = (ctx) => {
  if (ctx.url.pathname === "/foo") {
    return new Response("Foo");
  }
};

const barHandler = (ctx) => {
  if (ctx.url.pathname === "/bar") {
    return new Response("Bar");
  }
};

export default compose(
  urlParser,
  poweredBy,
  homeHandler,
  fooHandler,
  barHandler,
);

当与 compose 函数一起使用时,处理程序可以返回或抛出 Response,或任何具有 toResponse 方法的对象。处理程序还可以设置 context.handleError 来处理未捕获的错误。

就是这样。这就是 Hattip API 的全部内容。其余部分都是类似于上述的中间件函数,它们添加了各种功能和发展工具,以简化您的生活。

软件包

Hattip 具有极高的模块化特性,因此您可以按需使用少量或大量功能:

基于 Vite 的零配置开发环境也在开发中。

贡献者

致谢

MIT 许可证