Path Parameters
Route parameters allow you to capture dynamic segments from the URL path using the:param syntax:
Getting All Parameters
You can retrieve all parameters at once as an object:src/request.ts:102, the param() method can be called with or without a key:
URL Decoding
Parameters are automatically URL-decoded:src/request.ts:109 handles this automatically:
Multiple Parameters
You can have multiple parameters in a single route:Optional Parameters
Mark parameters as optional by adding a? after the segment:
Optional parameters are handled by the router during route registration, creating multiple route entries.
checkOptionalParameter utility in src/utils/url.ts handles this:
Pattern Constraints
Use regular expressions to constrain parameter values:src/router/pattern-router/router.ts:24, patterns are converted to regex:
Wildcard Parameters
The* wildcard matches any path segment(s):
Combining Parameters with Wildcards
Catch-All Routes
A single* catches all routes:
Parameter Matching Behavior
The router implementation determines how parameters are matched. From the router interface insrc/router.ts:51:
Match Result Structure
The result of matching includes handlers and their parameter mappings:Router-Specific Behavior
Different routers handle parameters with varying performance characteristics:- RegExpRouter: Builds regex patterns for efficient matching
- TrieRouter: Uses a trie data structure for prefix matching
- LinearRouter: Iterates through routes sequentially
- PatternRouter: Uses regex with named capture groups
Type Safety
With TypeScript, Hono provides type-safe parameter access:Best Practices
Use descriptive parameter names
Use descriptive parameter names
Choose parameter names that clearly indicate their purpose:
Validate parameter values
Validate parameter values
Use pattern constraints or validation middleware:
Handle missing parameters gracefully
Handle missing parameters gracefully
Always check for undefined when accessing optional parameters: