Scripts2026年7月1日·1 分钟阅读

Polka — Micro Web Server for Node.js

Polka is a fast, minimal Node.js web server that provides Express-compatible routing in a package under 5 KB, making it suitable for microservices and lightweight HTTP handlers.

Agent 就绪

Agent 可直接安装

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Polka Web Server
直接安装命令
npx -y tokrepo@latest install 2fbab3e6-7569-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

Polka is a micro web server for Node.js that delivers Express-like routing in a tiny footprint. It uses a Trouter-based path matcher for fast route resolution and maintains API compatibility with Express middleware, so existing middleware like cors, helmet, and body-parser work out of the box.

What Polka Does

  • Routes HTTP requests with Express-compatible method handlers (get, post, put, delete)
  • Resolves routes using Trouter, a lightweight trie-based path matcher
  • Supports middleware via .use() with the same (req, res, next) signature as Express
  • Handles parameterized routes and wildcard patterns
  • Returns a native http.Server, allowing direct access to Node.js server methods

Architecture Overview

Polka wraps Node.js's built-in http.createServer with a thin routing layer. Incoming requests are matched against a trie of registered routes via the Trouter library. Matched handlers execute in sequence with middleware running first. There is no built-in body parser, template engine, or view layer — Polka provides only the router and middleware pipeline, keeping the total package size under 5 KB.

Self-Hosting & Configuration

  • Requires Node.js 12 or later
  • Install with npm install polka
  • Attach middleware with .use() before defining routes
  • Call .listen(port) to start; returns a native http.Server instance
  • Deploy behind a reverse proxy (Nginx, Caddy) for TLS and load balancing

Key Features

  • Under 5 KB total size, with no dependencies beyond Trouter
  • Express-compatible middleware signature for ecosystem compatibility
  • Trie-based routing is faster than Express's linear route matching
  • Returns a standard http.Server for direct integration with Node.js APIs
  • Supports async handlers via returned Promises or async functions

Comparison with Similar Tools

  • Express — the standard Node.js framework; Polka is 30-50x smaller with faster routing
  • Fastify — schema-based validation and plugin system; Polka is simpler and smaller
  • Koa — async-first with context objects; Polka keeps the Express req/res signature
  • Hono — multi-runtime edge framework; Polka targets Node.js specifically
  • uWebSockets.js — C++ backed for raw speed; Polka uses standard Node.js http

FAQ

Q: Can I use Express middleware with Polka? A: Yes. Polka supports the standard (req, res, next) middleware signature, so most Express middleware works without changes.

Q: Does Polka support TypeScript? A: Type definitions are available via @types/polka on npm.

Q: Is Polka suitable for production? A: Yes. Its small surface area and use of native http.Server make it production-ready when paired with a reverse proxy.

Q: How does Polka handle errors? A: Polka provides an onError handler and a onNoMatch handler for 404s. Middleware can call next(err) to trigger error handling.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产