Skip to main content

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:
From src/request.ts:102, the param() method can be called with or without a key:

URL Decoding

Parameters are automatically URL-decoded:
The decoding logic in 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.
The checkOptionalParameter utility in src/utils/url.ts handles this:

Pattern Constraints

Use regular expressions to constrain parameter values:
From 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:
Wildcard routes should typically be defined last, as they match broadly.

Parameter Matching Behavior

The router implementation determines how parameters are matched. From the router interface in src/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

Choose parameter names that clearly indicate their purpose:
Use pattern constraints or validation middleware:
Always check for undefined when accessing optional parameters: