Skip to main content

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 using app.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.
See the Router API Reference for detailed information about each router.

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.
The order of route registration matters! More specific routes should be registered before generic ones.

Wildcard Priority

Wildcard routes have the lowest priority.

Route Metadata

RouterRoute Interface

Each route is represented by a RouterRoute 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

Register more specific routes before generic wildcard routes to ensure correct matching.
Add regex patterns to route parameters to validate input at the routing level.
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

Type Safety

Typed Routes

Use TypeScript for type-safe routing.

Schema Definition

Define API schemas for type-safe client generation.