跳到正文

itty-router

维护中
kwhitley/itty-router

面向 Cloudflare Workers 等运行时的超小型 API 路由器——最小约 450 字节,支持路由参数、中间件和 TypeScript。

在 GitHub 查看
星标
2049
Fork
84
关注
2049
未关闭 Issue
14

该介绍目前仅提供英文版本。

Overview

itty-router is an ultra-tiny API router designed for environments where size matters — Cloudflare Workers above all. Its routers range from roughly 450 bytes to under 1 KB for the batteries-included AutoRouter, hundreds of times smaller than Express, while still offering route parameters, query parsing, middleware, and TypeScript support.

It has been a staple of the Workers ecosystem for years and also runs anywhere that speaks the standard Request / Response APIs, including Bun, Deno, and Node.js.

Key Features

  • Tiny: ~450 B for the core router, ~1 KB for AutoRouter with built-in JSON responses and error handling
  • TypeScript-first with flexible typing for any environment
  • Route patterns with params, wildcards, and optional segments, plus query parsing
  • Middleware — use the built-ins or write your own
  • 100% test coverage
  • No assumptions: return any value, pass anything through; even future HTTP methods work

Getting Started

npm install itty-router

A complete Worker:

import { AutoRouter } from "itty-router";

const router = AutoRouter();

router
  .get("/hello/:name", ({ name }) => `Hello, ${name}!`)
  .get("/json", () => [1, 2, 3]);

export default { ...router };

AutoRouter turns returned values into proper responses and handles 404s and errors for you. Full documentation for v5 (and a v4 to v5 migration guide) is available at itty.dev.

Use Cases

  • Small, fast JSON APIs on Cloudflare Workers
  • Edge middleware and proxies where bundle size affects cold starts
  • Pairing with chanfana to add OpenAPI schemas and validation

License

MIT License.

按共同分类和标签排序的同类开源项目。

全部 Web 框架项目 →