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
| Function | Signature | Description |
|---|---|---|
createRouter | () => NexgenRouter | Creates an OpenAPIHono router with group(), api(), and route() helpers |
router.group | (...middlewares) => NexgenRouter | Applies middleware to every route registered after it |
router.api | (route, handlerOrMiddlewares?, handler?) => NexgenRouter | Registers 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
createRouterreturns anOpenAPIHonoinstance (from@hono/zod-openapi), so all standard Hono methods (.get,.post,.middleware, …) are available.- Route auto-discovery imports the default export of each
routes/*.tsand mounts it under/api/<module>. api(route, middlewares, handler)throws ifhandleris missing while a middleware array is given.- Shorthand:
group(...)==createRouter().group(...).
Related
- createRoute · group · jsonContent · z · HttpStatusCodes
