英文:
Subrouter issues with negroni / gorilla mux
问题
所以我正在尝试设置我的路由器来响应/users
和/users/{userId}
,所以我尝试了这段代码:
usersRouter := router.PathPrefix("/users").Subrouter()
usersRouter.HandleFunc("", users.GetUsersRoute).Methods("GET")
usersRouter.HandleFunc("/{userId:[0-9]*}", users.GetUserRoute).Methods("GET")
问题是当我访问/users
时,我得到一个404错误(但是对/users/
有响应)。如果我这样做:
router.HandleFunc("/users", users.GetUsersRoute).Methods("GET")
router.HandleFunc("/users/{userId:[0-9]*}", users.GetUserRoute).Methods("GET")
它就像我想要的那样工作。
有没有办法使用子路由器使URL按照我想要的方式工作?
英文:
So I am trying to setup my router to respond to /users
and /users/{userId}
so I tried this code:
usersRouter := router.PathPrefix("/users").Subrouter()
usersRouter.HandleFunc("", users.GetUsersRoute).Methods("GET")
usersRouter.HandleFunc("/{userId:[0-9]*}", users.GetUserRoute).Methods("GET")
The issue is that I get a 404 error when I go to /users
(but is does respond to /users/
) If I do:
router.HandleFunc("/users", users.GetUsersRoute).Methods("GET")
router.HandleFunc("/users/{userId:[0-9]*}", users.GetUserRoute).Methods("GET")
It works like I want it to.
Is there any way to get the URLs to work like I want with Subrouters?
答案1
得分: 1
是的和不是。你可以通过在路由器中添加StrictSlash(true)来使路由部分工作。
给定以下代码
package main
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
func main() {
mainRouter := mux.NewRouter().StrictSlash(true)
mainRouter.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "test") })
subRouter := mainRouter.PathPrefix("/users").Subrouter()
subRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "/users") })
subRouter.HandleFunc("/{id:[0-9]+}", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "/users/id") })
http.ListenAndServe(":8080", mainRouter)
}
对http://localhost:8080/users的请求将返回
< HTTP/1.1 301 Moved Permanently
< Location: /users/
< Date: Tue, 07 Apr 2015 19:52:12 GMT
< Content-Length: 42
< Content-Type: text/html; charset=utf-8
<
<a href="/users/">Moved Permanently</a>.
对http://localhost:8080/users/的请求返回
< HTTP/1.1 200 OK
< Date: Tue, 07 Apr 2015 19:54:43 GMT
< Content-Length: 6
< Content-Type: text/plain; charset=utf-8
< /users
所以如果你的客户端是浏览器,那么这可能是可以接受的。
英文:
Yes and no. You can make the routes semi-work by adding StrictSlash(true) to the router.
Given the following code
package main
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
)
func main() {
mainRouter := mux.NewRouter().StrictSlash(true)
mainRouter.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "test") })
subRouter := mainRouter.PathPrefix("/users").Subrouter()
subRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "/users") })
subRouter.HandleFunc("/{id:[0-9]+}", func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "/users/id") })
http.ListenAndServe(":8080", mainRouter)
}
a request to http://localhost:8080/users will return
< HTTP/1.1 301 Moved Permanently
< Location: /users/
< Date: Tue, 07 Apr 2015 19:52:12 GMT
< Content-Length: 42
< Content-Type: text/html; charset=utf-8
<
<a href="/users/">Moved Permanently</a>.
a request to http://localhost:8080/users/ returns
< HTTP/1.1 200 OK
< Date: Tue, 07 Apr 2015 19:54:43 GMT
< Content-Length: 6
< Content-Type: text/plain; charset=utf-8
< /users
so if your client is a browser then perhaps this is acceptable.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论