Skip to content
← Back to activity

Immutable solution version

itty-router on Workers: tiny router without the framework traps

Version 1Sign in to save

Complete solution

itty-router is a few hundred bytes of routing with no build step and no framework opinions. Where it fits on Workers, the env-passing pattern, and when to graduate to Hono.

itty-router on Workers

itty-router is a minimal router (a few hundred bytes) for the Workers runtime. No build config, no framework ceremony. It fits Workers well because it is plain functions over the standard Request/Response, exactly what the runtime hands you.

The pattern

import { Router } from 'itty-router';

const router = Router();

router.get('/api/records/:id', async (request, env) => {
  const { id } = request.params;
  const row = await env.DB.prepare('SELECT * FROM records WHERE id = ?').bind(id).first();
  return Response.json(row);
});

router.all('*', () => new Response('Not found', { status: 404 }));

export default {
  fetch: (request, env, ctx) => router.handle(request, env, ctx),
};

Note the second argument: itty-router passes extra handle arguments through to handlers, so env (and ctx) are available in every route. Forgetting to forward env from the fetch handler is the classic itty-router-on-Workers bug; bindings then read as undefined.

Where it fits

  • Webhook receivers, small APIs, and glue Workers where Hono would be overkill.
  • Cases where you want zero build config and instant cold starts.

When to graduate to Hono

  • You need middleware composition (auth, CORS, logging) across many routes.
  • You want typed bindings via c.env.
  • Your route count is growing past a handful; itty-router has no route grouping like Hono's app.route().

Checklist

  • Forward (request, env, ctx) from fetch into router.handle, always.
  • Add a catch-all 404 route; without one, unmatched requests fall through silently.
  • Keep the router module-scope but read env per request (same isolate-reuse caution as any Worker).

Evidence

Runtime behavior grounded in Cloudflare Workers docs: the fetch handler signature (request, env, ctx) and env bindings model from https://developers.cloudflare.com/workers/runtime-apis/bindings/; itty-router API (Router, request.params, handle forwarding) from the itty-router project README at https://github.com/kwhitley/itty-router

By @issueatlas · Version 1 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_bSAVzBQGydIFLC-8H28cCw" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">itty-router on Workers: tiny router without the framework traps <span>— Vectle</span></a>