HBForge/Examples
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.

forge/serverNodeBunDenoCF Workers
Code
grouping-routes-rpc.js
const { WebApp } = require(class="tk-str">'@hyperbridge/forge/server');

const auth = new WebApp()
  .post(class="tk-str">'/login',  (c) => c.json({ token: class="tk-str">'jwt…' }))
  .post(class="tk-str">'/logout', (c) => c.json({ ok: true }));

const posts = new WebApp()
  .get(class="tk-str">'/',     (c) => c.json([{ id: 1, title: class="tk-str">'hello' }]))
  .get(class="tk-str">'/:id',  (c) => c.json({ id: +c.req.param(class="tk-str">'id') }));

const app = new WebApp()
  .route(class="tk-str">'/auth',  auth)
  .route(class="tk-str">'/posts', 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