Itty aims to be the world's smallest (~440 bytes), feature-rich JavaScript router, enabling beautiful API code with a near-zero bundlesize. Designed originally for Cloudflare Workers, itty can be used in browsers, Service Workers, edge functions, or standalone runtimes like Node, Bun, etc.!
- fully typed/TypeScript support
- route params (including optional params)
- wildcards and greedy params
- query parsing
- middleware
- router nesting
- lightweight, easy-to-understand route code
- works in virtually any runtime/environment (including the browser)
Complete docs/API are available at itty.dev, or join our Discord channel to chat!
npm install itty-router@next
import {
error, // creates error responses
json, // creates JSON responses
Router, // the ~440 byte router itself
withParams, // middleware: puts params directly on the Request
} from 'itty-router'
import { todos } from './external/todos'
// create a new Router
const router = Router()
router
// add some middleware upstream on all routes
.all('*', withParams)
// GET list of todos
.get('/todos', () => todos)
// GET single todo, by ID
.get('/todos/:id',
({ id }) => todos.getById(id) || error(404, 'That todo was not found')
)
// 404 for everything else
.all('*', () => error(404))
// Example: Cloudflare Worker module syntax
export default {
fetch: (request, ...args) => router
.handle(request, ...args)
.then(json) // send as JSON
.catch(error) // catch errors
}
Have a question? Suggestion? Complaint? Want to send me a gift basket?
Join us on Discord!
- Fork repo
- Install dev dependencies via
yarn
- Start test runner/dev mode
yarn dev
- Add your code and tests if needed - do NOT remove/alter existing tests
- Verify that tests pass once minified
yarn verify
- Commit files
- Submit PR with a detailed description of what you're doing
- I'll add you to the credits! :)
This repo goes out to my past and present colleagues at Arundo - who have brought me such inspiration, fun,
and drive over the last couple years. In particular, the absurd brevity of this code is thanks to a
clever [abuse] of Proxy
, courtesy of the brilliant @mvasigh.
This trick allows methods (e.g. "get", "post") to by defined dynamically by the router as they are requested,
drastically reducing boilerplate.
These folks are the real heroes, making open source the powerhouse that it is! Help out and get your name added to this list! <3
- @mvasigh - proxy hack wizard behind itty, coding partner in crime, maker of the entire doc site, etc, etc.
- @hunterloftis - router.handle() method now accepts extra arguments and passed them to route functions
- @SupremeTechnopriest - improved TypeScript support and documentation! :D
- @taralx - router internal code-golfing refactor for performance and character savings
- @DrLoopFall - v4.x re-minification
- @taralx - QOL fixes for contributing (dev dep fix and test file consistency) <3
- @technoyes - three kind-of-a-big-deal errors fixed. Imagine the look on my face... thanks man!! :)
- @roojay520 - TS interface fixes
- @jahands - v4.x TS fixes