Zum Inhalt springen

Route-Helfer ​

Der Route-Helfer bietet erweiterte Routing-Informationen für die Fehlersuche und die Entwicklung von Middleware. Du kannst damit auf detaillierte Informationen zu übereinstimmenden Routen und der aktuell verarbeiteten Route zugreifen.

Import ​

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

Verwendung ​

Grundlegende Routeninformationen ​

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,
  })
})

Mit Unteranwendungen arbeiten ​

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() ​

Gibt ein Array aller Routen zurück, die zur aktuellen Anfrage passen, einschließlich 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() ​

Gibt das für den aktuellen Handler registrierte Routenpfadmuster zurück.

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

Mit einem Indexparameter verwenden ​

Du kannst optional einen Indexparameter übergeben, um ähnlich wie bei Array.prototype.at() den Routenpfad an einer bestimmten Position abzurufen.

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() ​

Gibt das im Routing angegebene Basispfadmuster der aktuellen Route zurück.

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

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

Mit einem Indexparameter verwenden ​

Du kannst optional einen Indexparameter übergeben, um den Basisroutenpfad an einer bestimmten Position abzurufen, ähnlich wie bei 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() ​

Gibt den Basispfad mit den eingesetzten Parametern aus der tatsächlichen Anfrage zurück.

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)

Veröffentlicht unter der MIT-Lizenz.