Import
Getting Matched Routes
matchedRoutes
Retrieve all routes that matched the current request, including middleware and handlers:RouterRoute[] - Array of matched route objects containing:
path: The route pattern (e.g.,/posts/:id)method: HTTP method (e.g.,GET,POST)handler: The handler functionbasePath: The base path of the route
The routes are returned in the order they were matched, with middleware first followed by the final handler.
Getting Route Paths
routePath
Get the route pattern that was registered for the current request:Context
required
The Hono context object
number
The index of the route to retrieve. Supports negative indices (counted from the end). Defaults to the current route index.
string - The route pattern, or empty string if index is out of bounds
baseRoutePath
Get the raw base path of the route as registered, including any parameters:Context
required
The Hono context object
number
The index of the route to retrieve. Supports negative indices. Defaults to the current route index.
string - The raw base path with parameter placeholders
basePath
Get the actual base path with parameter values filled in:Context
required
The Hono context object
number
The index of the route to retrieve. Supports negative indices. Defaults to the current route index.
string - The resolved base path with actual parameter values
basePath results are cached for performance. The function intelligently resolves parameter values from the actual request path.Use Cases
Request Logging
Log all matched routes for debugging:Dynamic Routing Logic
Implement logic based on the current route:Sub-application Context
Determine which sub-application is handling the request:Accessing Previous Middleware Routes
Inspect which middleware have already run:Understanding Route Index
Theindex parameter works similar to Array.prototype.at():
Best Practices
Use for Logging
Perfect for request logging and debugging matched routes
Cache Aware
basePath caches results automatically for optimal performanceMiddleware Inspection
Use
matchedRoutes to inspect the entire middleware chainNegative Indices
Use negative indices to access routes from the end of the match list