Skip to content

Routing

Define routes with c.chemin(...):

router.route(c.chemin("users", c.pNumber("id")), "GET", ({ params, resp }) => {
resp.json({ id: params.get("id") })
})

Useful path pieces:

  • c.pString("name")
  • c.pNumber("id")
  • c.pOptionalConst("delete")
  • c.pOptional(...)

Single method:

router.route(c.chemin("users"), "GET", ({ resp }) => resp.json([]))

Multiple methods:

router.route(c.chemin("users"), ["GET", "POST"], ({ resp }) => resp(new Response("ok")))

Method map:

router.route(c.chemin("users"), {
GET: ({ resp }) => resp.json([]),
POST: ({ resp }) => resp(new Response(null, { status: 201 })),
})

Wildcard method:

router.route(c.chemin("health"), "*", ({ resp }) => resp(new Response("ok")))
const api = router.router(c.chemin("api"))
const posts = api.router(c.chemin("posts"))
posts.route(c.chemin(c.pNumber("id")), "GET", ({ params, resp }) => {
resp.json({ id: params.get("id") })
})

Nested routers preserve path typing while composing larger route trees.