Skip to content
Discussion options

You must be logged in to vote

The earlier /users/:id:activate suggestion is the part to avoid. In Hono, :id:activate is parsed as one parameter name, not as :id plus a literal :activate.

Use an explicit regexp on the parameter instead:

app.post('/users/:id{[^:]+}:activate', (c) => {
  const id = c.req.param('id')
  return c.json({ id, action: 'activate' })
})

app.post('/users/:id{[^:]+}:deactivate', (c) => {
  const id = c.req.param('id')
  return c.json({ id, action: 'deactivate' })
})

The {[^:]+} part makes id stop before the literal colon. If you have many actions, a single route like /users/:idAction and splitting on : in the handler is simpler, but for a small fixed set the regexp route keeps the handlers explicit.

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by is-jonreeves
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants