Route

Routing

routes are agnostic to what router is used upstream. Since routes are standlone, we have many options for handling routing.


Filesystem Routing

Webroute shines in filesystem router contexts, where middleware is either unavailable or especially awkward. Additionally, routes are very lightweight, only loading the necessary code for each route invocation.

webroute works idiomatically with filesystem routers.

posts/route.ts
// GET /posts
export const GET = route("/ignored-path") // <- This pathname will be ignored
  .method("put") // <- And so will this method
  .handle(() => {});

@webroute/router

We can use routes with @webroute/router by registering routes.

import { createRadixRouter } from "@webroute/router";
 
const router = createRadixRouter([
  {
    path: "/foo",
    methods: ["GET"],
    payload: myRoute,
  },
]);

Or if we have many routes, we can use the normalizeRoutes method.

import { normaliseRoutes } from "@webroute/route";
 
const appRoutes = { myRoute, myOtherRoute };
const routes = normaliseRoutes(appRoutes);
 
const router = createRadixRouter(routes);
 
Bun.serve({
  fetch(req) {
    const handler = router.match(req);
    return handler?.(request) ?? RESPONSE_NOT_FOUND;
  },
});

Learn more about the Router package.

No Routing

In smaller projects this approach is sufficient. However, as the number of routes or path complexity increases, you'll likely want to use a more sophisticated routing approach.

In many cases, you don't actually need a fancy router at all. It's completely valid to export a single route and call it a day.

Bun.serve({
  fetch: myRoute(req),
});
Helpers

The Route.getPath and Route.getMethods helpers, found on the route export, are helpful for building custom routing.

Third Party Frameworks

In more sophisticated apps, or if integrating with an existing app, we may wish to use an external router/framework.

One-by-one

In the most basic case, we can simply wire up our routes explicitly and manually.

hono-example.ts
const app = new Hono();
 
app.on(
  Route.getMethods(myRoute), // METHODS
  Route.getPath(myRoute), // PATH
  myRoute // HANDLER
);

Many at Once

We can also perform this iteratively when we have quite a few routes.

hono-example.ts
// ---cut-start---
import { Hono } from "hono";
import { route, Route } from "@webroute/route";
const routes = [route().handle(() => {})];
// ---cut-end---
const app = new Hono();
 
// For every route, map each method to the route handler
for (const r of routes) {
  app.on(Route.getMethods(r), Route.getPath(r), (c) => r(c.req.raw));
}
 
// This is a completely valid, production ready "hono adapter"
export default app;

Bear in mind most external routers care about route registration order.

What about adapters?

As we've seen above, connecting our routes to existing apps is fairly straightforward: merely register the apps path and method(s) with the route itself. Instead of providing and maintaining adapters, we think it's more robust to leave that up to the developer.

Ultimately, by writing your own "adapter" code, you will retain greater control, with pretty minimal work.

More guidance on integrating with existing frameworks and apps can be found in the Building Apps section.

On this page