06 · Applications
Mount sub-apps under namespaces
Each sub-app has its own router and middleware. app.route() composes them into a single tree without runtime overhead.
Code
grouping-routes-rpc.js
const { WebApp } = require(class="tk-str">39;@hyperbridge/forge/server39;); const auth = new WebApp() .post(class="tk-str">39;/login39;, (c) => c.json({ token: class="tk-str">39;jwt…39; })) .post(class="tk-str">39;/logout39;, (c) => c.json({ ok: true })); const posts = new WebApp() .get(class="tk-str">39;/39;, (c) => c.json([{ id: 1, title: class="tk-str">39;hello39; }])) .get(class="tk-str">39;/:id39;, (c) => c.json({ id: +c.req.param(class="tk-str">39;id39;) })); const app = new WebApp() .route(class="tk-str">39;/auth39;, auth) .route(class="tk-str">39;/posts39;, posts); app.listen(3000);
How it works
app.route(prefix, subApp) walks the sub-app's router and remounts every entry under the prefix. It also carries over middleware and inherits error/notFound handlers when the parent has none.
Each sub-app is a self-contained module — easy to test in isolation, ship as a package, or mount under different prefixes in different environments.
Because remounting copies the trie, lookups stay O(path-depth) — no runtime cost for the composition.
Try it
Quickstart
curl http://localhost:3000/posts/42 ; curl http://localhost:3000/auth/login -X POST
Related modules
This is HBForge's port of Hono's example. Read the original at hono.dev/examples/grouping-routes-rpc.