> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/honojs/hono/llms.txt
> Use this file to discover all available pages before exploring further.

# Route

> Inspect and access route information from matched handlers

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

```typescript theme={null}
import { matchedRoutes, routePath, baseRoutePath, basePath } from 'hono/route'
```

## Getting Matched Routes

### matchedRoutes

Retrieve all routes that matched the current request, including middleware and handlers:

```typescript theme={null}
import { matchedRoutes } from 'hono/route'

app.use('*', async (c, next) => {
  await next()
  
  matchedRoutes(c).forEach(({ handler, method, path }, i) => {
    const name = handler.name || (handler.length < 2 ? '[handler]' : '[middleware]')
    console.log(
      method,
      ' ',
      path,
      ' '.repeat(Math.max(10 - path.length, 0)),
      name,
      i === c.req.routeIndex ? '<- respond from here' : ''
    )
  })
})
```

**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

<Note>
  The routes are returned in the order they were matched, with middleware first followed by the final handler.
</Note>

## Getting Route Paths

### routePath

Get the route pattern that was registered for the current request:

```typescript theme={null}
import { routePath } from 'hono/route'

app.use('*', (c, next) => {
  console.log(routePath(c))     // '*'
  console.log(routePath(c, -1)) // '/posts/:id'
  return next()
})

app.get('/posts/:id', (c) => {
  return c.text(routePath(c)) // '/posts/:id'
})
```

**Parameters**:

<ParamField path="c" type="Context" required>
  The Hono context object
</ParamField>

<ParamField path="index" type="number">
  The index of the route to retrieve. Supports negative indices (counted from the end). Defaults to the current route index.
</ParamField>

**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:

```typescript theme={null}
import { baseRoutePath } from 'hono/route'

const app = new Hono()

const subApp = new Hono()
subApp.get('/posts/:id', (c) => {
  return c.text(baseRoutePath(c)) // '/:sub'
})

app.route('/:sub', subApp)
```

**Parameters**:

<ParamField path="c" type="Context" required>
  The Hono context object
</ParamField>

<ParamField path="index" type="number">
  The index of the route to retrieve. Supports negative indices. Defaults to the current route index.
</ParamField>

**Returns**: `string` - The raw base path with parameter placeholders

### basePath

Get the actual base path with parameter values filled in:

```typescript theme={null}
import { basePath } from 'hono/route'

const app = new Hono()

const subApp = new Hono()
subApp.get('/posts/:id', (c) => {
  return c.text(basePath(c)) // '/api' (if request was to /api/posts/123)
})

app.route('/:sub', subApp)
```

**Parameters**:

<ParamField path="c" type="Context" required>
  The Hono context object
</ParamField>

<ParamField path="index" type="number">
  The index of the route to retrieve. Supports negative indices. Defaults to the current route index.
</ParamField>

**Returns**: `string` - The resolved base path with actual parameter values

<Note>
  `basePath` results are cached for performance. The function intelligently resolves parameter values from the actual request path.
</Note>

## Use Cases

### Request Logging

Log all matched routes for debugging:

```typescript theme={null}
import { matchedRoutes } from 'hono/route'

app.use('*', async (c, next) => {
  const start = Date.now()
  await next()
  const ms = Date.now() - start
  
  const routes = matchedRoutes(c)
  console.log(`${c.req.method} ${c.req.path} - ${ms}ms - ${routes.length} handlers`)
})
```

### Dynamic Routing Logic

Implement logic based on the current route:

```typescript theme={null}
import { routePath } from 'hono/route'

app.get('/api/*', (c) => {
  const pattern = routePath(c)
  
  if (pattern === '/api/*') {
    return c.json({ error: 'Not found' }, 404)
  }
  
  return c.json({ path: pattern })
})
```

### Sub-application Context

Determine which sub-application is handling the request:

```typescript theme={null}
import { basePath } from 'hono/route'

const apiApp = new Hono()
apiApp.get('/users', (c) => {
  const base = basePath(c) // '/api/v1'
  return c.json({ base, message: 'Users endpoint' })
})

app.route('/api/v1', apiApp)
```

### Accessing Previous Middleware Routes

Inspect which middleware have already run:

```typescript theme={null}
import { routePath } from 'hono/route'

app.use('/admin/*', authMiddleware)

app.get('/admin/users', (c) => {
  // Get the middleware route pattern
  const middlewarePath = routePath(c, 0) // '/admin/*'
  const handlerPath = routePath(c, 1)    // '/admin/users'
  
  return c.json({ middlewarePath, handlerPath })
})
```

## Understanding Route Index

The `index` parameter works similar to `Array.prototype.at()`:

```typescript theme={null}
import { routePath } from 'hono/route'

// Assuming routes: ['*', '/api/*', '/api/users']

app.get('/api/users', (c) => {
  routePath(c)     // Current route: '/api/users'
  routePath(c, 0)  // First route: '*'
  routePath(c, 1)  // Second route: '/api/*'
  routePath(c, 2)  // Third route: '/api/users'
  routePath(c, -1) // Last route: '/api/users'
  routePath(c, -2) // Second to last: '/api/*'
  routePath(c, 99) // Out of bounds: ''
})
```

## Best Practices

<CardGroup cols={2}>
  <Card title="Use for Logging" icon="file-lines">
    Perfect for request logging and debugging matched routes
  </Card>

  <Card title="Cache Aware" icon="gauge-high">
    `basePath` caches results automatically for optimal performance
  </Card>

  <Card title="Middleware Inspection" icon="layer-group">
    Use `matchedRoutes` to inspect the entire middleware chain
  </Card>

  <Card title="Negative Indices" icon="arrow-left">
    Use negative indices to access routes from the end of the match list
  </Card>
</CardGroup>

## Type Definitions

```typescript theme={null}
interface RouterRoute {
  path: string
  method: string
  handler: Function
  basePath: string
}

function matchedRoutes(c: Context): RouterRoute[]
function routePath(c: Context, index?: number): string
function baseRoutePath(c: Context, index?: number): string
function basePath(c: Context, index?: number): string
```
