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.