使用negroni和gorilla mux时的子路由问题

huangapple go评论93阅读模式
英文:

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 (
    	&quot;fmt&quot;
    	&quot;net/http&quot;
    
    	&quot;github.com/gorilla/mux&quot;
    )
    
    func main() {
    	mainRouter := mux.NewRouter().StrictSlash(true)
    	mainRouter.HandleFunc(&quot;/test&quot;, func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, &quot;test&quot;) })
    
    	subRouter := mainRouter.PathPrefix(&quot;/users&quot;).Subrouter()
    	subRouter.HandleFunc(&quot;/&quot;, func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, &quot;/users&quot;) })
    	subRouter.HandleFunc(&quot;/{id:[0-9]+}&quot;, func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, &quot;/users/id&quot;) })
    	http.ListenAndServe(&quot;:8080&quot;, mainRouter)
    }

a request to http://localhost:8080/users will return

&lt; HTTP/1.1 301 Moved Permanently
&lt; Location: /users/
&lt; Date: Tue, 07 Apr 2015 19:52:12 GMT
&lt; Content-Length: 42
&lt; Content-Type: text/html; charset=utf-8
&lt; 
&lt;a href=&quot;/users/&quot;&gt;Moved Permanently&lt;/a&gt;.

a request to http://localhost:8080/users/ returns

&lt; HTTP/1.1 200 OK
&lt; Date: Tue, 07 Apr 2015 19:54:43 GMT
&lt; Content-Length: 6
&lt; Content-Type: text/plain; charset=utf-8

&lt; /users

so if your client is a browser then perhaps this is acceptable.

huangapple
  • 本文由 发表于 2015年4月7日 09:53:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/29482453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定