Base Path
ThebasePath() method allows you to create a new Hono instance with a prefixed base path:
src/hono-base.ts:247, basePath() creates a new instance with a merged path:
Mounting Routes with route()
The route() method allows you to mount a Hono instance at a specific path:
Implementation
Fromsrc/hono-base.ts:208, route() mounts all routes from the sub-app:
Organizing by Feature
Group related routes into separate modules:Nested Routing
You can nest routes multiple levels deep:Accessing Base Path Information
Use thehono/route helper to access base path information:
src/helper/route/index.ts:106, basePath() returns the base path with embedded parameters:
Using Middleware with Groups
Apply middleware to grouped routes:Grouping with use()
The use() method can apply middleware to multiple routes:
src/hono-base.ts:157, the use() implementation:
Versioned APIs
Organize different API versions:Dynamic Base Paths
Use parameters in base paths for multi-tenant applications:Mounting External Applications
For mounting non-Hono applications, usemount():
src/hono-base.ts:328, the mount() method:
Best Practices
Organize by domain
Organize by domain
Group routes by business domain or feature:
Use consistent base paths
Use consistent base paths
Establish a clear pattern for your API structure:
Apply middleware at the group level
Apply middleware at the group level
Share middleware across related routes:
Keep route files focused
Keep route files focused
Each route module should handle a single resource or feature:
Route grouping helps maintain clean, modular code and makes it easier to apply middleware and organize your application.