Route multiple paths to same handler. #992
-
I'm looking to see if it is possible to route multiple paths with duplicate params to the same handler
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Hi, @msuntharesan! When defined as follows
The value of
Therefore, the hono does not allow such routing definitions. |
Beta Was this translation helpful? Give feedback.
-
Hi I'm having a similar issue. The following code throws a duplicate param error: import { Hono } from "hono";
new Hono()
.get("/:id", (c) => c.text(`${c.req.param("id")} in root`))
.get("/foo/:id", (c) => c.text(`${c.req.param("id")} in foo`))
.request("/foo/bar"); file:///Users/ynkt/tmp/hono-route-common-key/node_modules/hono/dist/router/trie-router/node.js:56
throw new Error(errorMessage(pattern[1]));
^
Error: Duplicate param name, use another name instead of 'id' - GET /foo/:id <--- 'id'
at Node.insert (file:///Users/ynkt/tmp/hono-route-common-key/node_modules/hono/dist/router/trie-router/node.js:56:21)
at TrieRouter.add (file:///Users/ynkt/tmp/hono-route-common-key/node_modules/hono/dist/router/trie-router/router.js:16:15)
at file:///Users/ynkt/tmp/hono-route-common-key/node_modules/hono/dist/router/smart-router/router.js:27:18
at Array.forEach (<anonymous>)
at SmartRouter.match (file:///Users/ynkt/tmp/hono-route-common-key/node_modules/hono/dist/router/smart-router/router.js:26:16)
at Hono.matchRoute (file:///Users/ynkt/tmp/hono-route-common-key/node_modules/hono/dist/hono.js:145:24)
at Hono.dispatch (file:///Users/ynkt/tmp/hono-route-common-key/node_modules/hono/dist/hono.js:156:25)
at Hono.fetch (file:///Users/ynkt/tmp/hono-route-common-key/node_modules/hono/dist/hono.js:41:19)
at Hono.request (file:///Users/ynkt/tmp/hono-route-common-key/node_modules/hono/dist/hono.js:53:25)
at file:///Users/ynkt/tmp/hono-route-common-key/src/index.js:6:4 I understand the purpose of this error but don't think the routings registered in my example are ambiguous. Am I overlooking something? |
Beta Was this translation helpful? Give feedback.
Hi, @msuntharesan!
When defined as follows
The value of
c.param('p1')
is ambiguous for the following request.Therefore, the hono does not allow such routing definitions.