Skip to content

路由助手

🌐 Route Helper

路由助手提供增强的路由信息,用于调试和中间件开发。它允许你访问关于匹配路由和当前正在处理的路由的详细信息。

🌐 The Route Helper provides enhanced routing information for debugging and middleware development. It allows you to access detailed information about matched routes and the current route being processed.

导入

🌐 Import

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

用法

🌐 Usage

基本路由信息

🌐 Basic route information

ts
const app = new Hono()

app.get('/posts/:id', (c) => {
  const currentPath = routePath(c) // '/posts/:id'
  const routes = matchedRoutes(c) // Array of matched routes

  return c.json({
    path: currentPath,
    totalRoutes: routes.length,
  })
})

与子应用协同工作

🌐 Working with sub-applications

ts
const app = new Hono()
const apiApp = new Hono()

apiApp.get('/posts/:id', (c) => {
  return c.json({
    routePath: routePath(c), // '/posts/:id'
    baseRoutePath: baseRoutePath(c), // '/api'
    basePath: basePath(c), // '/api' (with actual params)
  })
})

app.route('/api', apiApp)

matchedRoutes()

返回与当前请求匹配的所有路由(包括中间件)的数组。

🌐 Returns an array of all routes that matched the current request, including middleware.

ts
app.all('/api/*', (c, next) => {
  console.log('API middleware')
  return next()
})

app.get('/api/users/:id', (c) => {
  const routes = matchedRoutes(c)
  // Returns: [
  //   { method: 'ALL', path: '/api/*', handler: [Function] },
  //   { method: 'GET', path: '/api/users/:id', handler: [Function] }
  // ]
  return c.json({ routes: routes.length })
})

routePath()

返回为当前处理程序注册的路由路径模式。

🌐 Returns the route path pattern registered for the current handler.

ts
app.get('/posts/:id', (c) => {
  console.log(routePath(c)) // '/posts/:id'
  return c.text('Post details')
})

使用索引参数

🌐 Using with index parameter

你可以选择传递一个索引参数,以获取特定位置的路由路径,类似于 Array.prototype.at()

🌐 You can optionally pass an index parameter to get the route path at a specific position, similar to Array.prototype.at().

ts
app.all('/api/*', (c, next) => {
  return next()
})

app.get('/api/users/:id', (c) => {
  console.log(routePath(c, 0)) // '/api/*' (first matched route)
  console.log(routePath(c, -1)) // '/api/users/:id' (last matched route)
  return c.text('User details')
})

baseRoutePath()

返回路由中指定的当前路由的基本路径模式。

🌐 Returns the base path pattern of the current route as specified in routing.

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

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

使用索引参数

🌐 Using with index parameter

你可以选择传递一个索引参数,以获取特定位置的基本路由路径,类似于 Array.prototype.at()

🌐 You can optionally pass an index parameter to get the base route path at a specific position, similar to Array.prototype.at().

ts
app.all('/api/*', (c, next) => {
  return next()
})

const subApp = new Hono()
subApp.get('/users/:id', (c) => {
  console.log(baseRoutePath(c, 0)) // '/' (first matched route)
  console.log(baseRoutePath(c, -1)) // '/api' (last matched route)
  return c.text('User details')
})

app.route('/api', subApp)

basePath()

返回实际请求中嵌入参数的基本路径。

🌐 Returns the base path with embedded parameters from the actual request.

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

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

Hono 中文网 - 粤ICP备13048890号