Routing
Hono provides a flexible and powerful routing system that supports various path patterns, parameters, and multiple router implementations.Route Patterns
Basic Routes
Define routes using simple string paths.Path Parameters
Capture dynamic segments in the URL path.Wildcard Routes
Match multiple path segments.Regular Expressions
Use regex patterns in routes.Multiple Paths
Register the same handler for multiple paths usingapp.on().
Route Methods
HTTP Methods
Hono supports all standard HTTP methods.All Methods
Match all HTTP methods for a route.Custom Methods
Handle custom HTTP methods.Route Grouping
Using basePath
Create a Hono instance with a base path prefix.Using route
Mount sub-applications to group related routes.Router Configuration
Router Options
Configure the router when creating a Hono instance.Available Routers
Hono includes several router implementations:RegExpRouter
Fast regex-based router. Good for most use cases. Default in SmartRouter.
TrieRouter
Trie-based router. Excellent for apps with many routes. Default in SmartRouter.
SmartRouter
Combines multiple routers for optimal performance. Default router in Hono.
LinearRouter
Simple linear search router. Good for simple apps with few routes.
PatternRouter
URLPattern-based router. Uses the web standard URLPattern API.
Strict Mode
Control whether trailing slashes matter in route matching.Custom Path Function
Customize how paths are extracted from requests.Route Priority
Matching Order
Routes are matched in the order they are registered.Wildcard Priority
Wildcard routes have the lowest priority.Route Metadata
RouterRoute Interface
Each route is represented by aRouterRoute object.
Accessing Routes
Get all registered routes from the app.Route Matching
Match Results
The router returns match results containing handlers and parameters.Parameter Extraction
Parameters are extracted and decoded automatically.Best Practices
Use specific routes before wildcards
Use specific routes before wildcards
Register more specific routes before generic wildcard routes to ensure correct matching.
Use regex patterns for validation
Use regex patterns for validation
Add regex patterns to route parameters to validate input at the routing level.
Choose the right router
Choose the right router
For most applications, the default SmartRouter is optimal. Consider specific routers for specialized needs:
- LinearRouter for very simple apps
- RegExpRouter for regex-heavy patterns
- TrieRouter for apps with many routes