Skip to content

createRouter — the module router

Imported from the facade: import { createRouter } from "@/framework/facade.js".

Creates the Hono-based router every module route file exports. It adds group() (middleware for all later routes), api() (register a createRoute with an optional middleware array), and route() (mount sub-routers). See Routing.

Signature

FunctionSignatureDescription
createRouter() => NexgenRouterCreates an OpenAPIHono router with group(), api(), and route() helpers
router.group(...middlewares) => NexgenRouterApplies middleware to every route registered after it
router.api(route, handlerOrMiddlewares?, handler?) => NexgenRouterRegisters a createRoute; accepts an optional middleware array

Use cases

Public vs protected groups

ts
const publicRoutes = createRouter().group(loginLimiter).api(registerRoute, register);
const protectedRoutes = createRouter().group(authMiddleware).api(meRoute, me);

export default createRouter()
  .route("/", publicRoutes)
  .route("/", protectedRoutes);

Real world — the auth router

ts
// modules/auth/routes/api.ts
const publicRoute = createRouter()
  .group(loginLimiter)
  .api(loginRoute, login)
  .api(forgotPasswordRoute, forgotPassword);

const protectedRoute = createRouter()
  .group(authMiddleware)
  .api(usersIndexRoute, [requireRole("supreme", "superadmin", "admin")], usersIndex)
  .api(userDestroyRoute, [requireRole("supreme", "superadmin")], userDestroy);

export default createRouter().route("/", publicRoute).route("/", protectedRoute);

Plain routes without OpenAPI

When OPEN_API=false, skip createRoute and use verb methods directly:

ts
export default createRouter()
  .get("/", index)
  .get("/:id", show)
  .post("/", store)
  .put("/:id", update)
  .delete("/:id", destroy);

Notes

  • createRouter returns an OpenAPIHono instance (from @hono/zod-openapi), so all standard Hono methods (.get, .post, .middleware, …) are available.
  • Route auto-discovery imports the default export of each routes/*.ts and mounts it under /api/<module>.
  • api(route, middlewares, handler) throws if handler is missing while a middleware array is given.
  • Shorthand: group(...) == createRouter().group(...).

Released under the MIT License.