mux: Can't create "root" route on a subrouter
r := mux.NewRouter()
sr := r.PathPrefix("/foo")
sr.Path("/").HandlerFunc(...)
// GET /foo/ -> OK
// GET /foo -> 404 Not Found
I can’t find a way to get the second request to work. I thought StrictSlash
might help, but it doesn’t. Any suggestions?
About this issue
- Original URL
- State: closed
- Created 11 years ago
- Comments: 16 (6 by maintainers)
Commits related to this issue
- fix(discussion): mux subrouter issue https://github.com/gorilla/mux/issues/31 — committed to raystack/compass by mabdh 2 years ago
- feat: discussions feature (#87) * feat: add discussion feature * feat(discussion): validate size and offset with validator * feat(discussion): use expector in mock * feat(discussion): handle... — committed to raystack/compass by mabdh 2 years ago
Thanks for the reply. My example was slightly wrong, it seems. Have you tried your example? Because that’s what I was actually doing and it doesn’t work quite how I expect it to.
The behaviour is specifically just for
Path("/")
on a subrouter, it behaves differently toPath("/")
on a top-level router.Using your example:
When not using a top-level router:
This makes it difficult for me to make nesting independent routes. I want to be able to define something like:
See https://github.com/gorilla/mux/issues/215
Is there any update on this?
I have the situation where I want to handle just the path prefix for a subrouter, but I can’t unless there’s a trailing /. I think that’s the same as this issue.
Having a similar issue.
Router with strictslash = true, and seems to properly propagate to subrouter However, the path “/” on the sub redirects OK, but POSTs are being redirected to GET, which is bad
Is this a known issue?
Just for clarity: you’re not using a
Subrouter
that I can see anywhere…PathPrefix
doesn’t create aSubrouter
,Subrouter
does. Try doing this instead: