question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Specification of `*` and '/*' in routing

See original GitHub issue

I think the top-level * and /* behave a little differently than the documentation.

https://github.com/honojs/hono/releases/tag/v1.4.0

app.get('/', (c) => {
  return c.text('foo')
}) // <--- stop process

app.use('*', middleware()) // <--- not dispached!!

Currently, in both TrieRouter and RegExpRouter, * and /* have higher precedence than /. ( On TrieRouter, * has the highest priority, /* the next highest; on RegExpRouter, * and /* have the same priority. )

describe('star', () => {
  const router = new TrieRouter<string>()

  router.add('GET', '/', '/')
  router.add('GET', '/*', '/*')
  router.add('GET', '*', '*')

  router.add('GET', '/x', '/x')
  router.add('GET', '/x/*', '/x/*')

  it.only('top', async () => {
    const res = router.match('GET', '/')
    expect(res).not.toBeNull()
    expect(res.handlers).toEqual(['*', '/*', '/'])
  })

  it.only('Under a certain path', async () => {
    const res = router.match('GET', '/x')
    expect(res).not.toBeNull()
    expect(res.handlers).toEqual(['*', '/*', '/x', '/x/*'])
  })
})
describe('star', () => {
  const router = new RegExpRouter<string>()

  router.add('GET', '/', '/')
  router.add('GET', '/*', '/*')
  router.add('GET', '*', '*')

  router.add('GET', '/x', '/x')
  router.add('GET', '/x/*', '/x/*')

  it.only('top', async () => {
    const res = router.match('GET', '/')
    expect(res).not.toBeNull()
    expect(res.handlers).toEqual(['*', '/*', '/']) // => failed ['/*', '*', '/']
  })

  it.only('Under a certain path', async () => {
    const res = router.match('GET', '/x')
    expect(res).not.toBeNull()
    expect(res.handlers).toEqual(['*', '/*', '/x', '/x/*'])
  })
})

The scores appear to be as follows.

at TrieRouter

router.add('GET', '/', '/')  // score: 1.1
router.add('GET', '/*', '/*') // score: 0.2
router.add('GET', '*', '*') // score: 0.3 - 1 => Approx. -0.7

at RegExpRouter

router.add('GET', '/', '/')  // score: 1.1
router.add('GET', '/*', '/*') // score: 0.2 in RegExpRouter "/*" is an alias to "*".
router.add('GET', '*', '*') // score: 0.3

I think the current specification for TrieRouter is acceptable to us as users, so I can make it the official specification and adapt RegExpRouter to it. Alternatively, we can treat ‘/’ as an alias to '’, which would be the same as the RegExpRouter implementation.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
usualomacommented, May 26, 2022

@yusukebe We did it! I believe this will put an end to the long struggle in May. Thanks to user feedback, I’m sure we’ve landed on a very good spec!

0reactions
yusukebecommented, May 26, 2022

@usualoma

I’ve merged two PRs #275, #276 to master. I tried the graghql scenario, it works well!

import { buildSchema } from 'graphql'
import { Hono } from 'hono'
import { graphqlServer } from 'hono/graphql-server'

const app = new Hono()

const schema = buildSchema(`
type Query {
  hello: String
}
`)

const rootValue = {
  hello: () => 'Hello Hono!',
}

app.use(
  '/graphql',
  graphqlServer({
    schema,
    rootValue,
  })
)

app.get('*', (c) => {
  return c.text('fallback')
})

export default app

If there is no problem, I’ll release v1.4.1.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Routing Engine Specifications
Table 1 lists the current specifications for Routing Engines supported on M Series, MX Series, and T Series routers. Table 2 lists the...
Read more >
Routing Policy Specification Language
The Routing Policy Specification Language (RPSL) is a language commonly used by Internet Service Providers to describe their routing policies.
Read more >
Order Routing and Handling Data Technical Specification
The Technical Specification defines syntax of data elements, their contents, element arrangements, validation methods and dissemination methods ...
Read more >
Network Routers Specifications
Help with Network Routers specifications: ; Number of Ports, The total number of networking ports which the network equipment provides. ; Search Logic:...
Read more >
RFC 2280 - Routing Policy Specification Language (RPSL)
Routing Policy Specification Language (RPSL) (RFC 2280, January 1998; ... RPSL is extensible; new routing protocols and new protocol features can be ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found