Skip to main content
The Route helper provides utilities for inspecting matched routes and retrieving route path information during request handling. This is useful for logging, debugging, and dynamic routing logic.

Import

Getting Matched Routes

matchedRoutes

Retrieve all routes that matched the current request, including middleware and handlers:
Returns: 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 function
  • basePath: 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:
Parameters:
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.
Returns: 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:
Parameters:
Context
required
The Hono context object
number
The index of the route to retrieve. Supports negative indices. Defaults to the current route index.
Returns: string - The raw base path with parameter placeholders

basePath

Get the actual base path with parameter values filled in:
Parameters:
Context
required
The Hono context object
number
The index of the route to retrieve. Supports negative indices. Defaults to the current route index.
Returns: 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

The index 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 performance

Middleware Inspection

Use matchedRoutes to inspect the entire middleware chain

Negative Indices

Use negative indices to access routes from the end of the match list

Type Definitions