Skip to main content
The Route Helper provides utilities for accessing information about matched routes and base paths.

Import

import { matchedRoutes, routePath, baseRoutePath, basePath } from 'hono/route'

Functions

matchedRoutes()

Get all matched routes for the current request.
function matchedRoutes(c: Context, index?: number): RouterRoute | RouterRoute[] | undefined
c
Context
required
The context object
index
number
Optional index to get a specific route (supports negative indices)
return
RouterRoute | RouterRoute[] | undefined
The matched route(s) or undefined if index is out of range
Example
import { matchedRoutes } from 'hono/route'

app.get('/api/:id', (c) => {
  const routes = matchedRoutes(c)
  console.log(routes) // All matched routes
  
  const last = matchedRoutes(c, -1)
  console.log(last?.path) // '/api/:id'
  
  return c.text('OK')
})

routePath()

Get the matched route path.
function routePath(c: Context): string | undefined
c
Context
required
The context object
return
string | undefined
The route path (e.g., ‘/api/:id’)

baseRoutePath()

Get the base route path from the routes array.
function baseRoutePath(c: Context): string | undefined
c
Context
required
The context object
return
string | undefined
The base route path

basePath()

Get the base path from the request.
function basePath(c: Context): string
c
Context
required
The context object
return
string
The base path (e.g., ‘/api’)

Types

type RouterRoute = [
  handler: H,
  method: string,
  path: string
]

Build docs developers (and LLMs) love