Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rest Go Gorilla Mux未按预期路由_Rest_Http_Go_Gorilla - Fatal编程技术网

Rest Go Gorilla Mux未按预期路由

Rest Go Gorilla Mux未按预期路由,rest,http,go,gorilla,Rest,Http,Go,Gorilla,我在获取Gorilla Mux库以便上班时遇到问题。从我所阅读的文档和我所做的所有调试来看,我似乎无法找出问题所在。以下是我对路由的了解: 文件夹结构: project_root |-- main.go |-- routes |-- routes.go |-- user.go main.go: package main import ( "fmt" "net/http" "./routes" ) func main()

我在获取Gorilla Mux库以便上班时遇到问题。从我所阅读的文档和我所做的所有调试来看,我似乎无法找出问题所在。以下是我对路由的了解:

文件夹结构:

project_root
  |-- main.go
  |-- routes
         |-- routes.go
         |-- user.go
main.go:

package main

import (
    "fmt"
    "net/http"

    "./routes"
)

func main() {
    r := routes.CreateRoutes(http.Dir("./content"))

    http.Handle("/", r)
    err := http.ListenAndServe(fmt.Sprintf("%s:%d", "localhost", 8000), nil)
    if err != nil {
        fmt.Println("Error: ", err)
    }
}
routes/routes.go

package routes

import (
    "net/http"

    "github.com/gorilla/mux"
)

func CreateRoutes(staticDir http.FileSystem) *mux.Router {
    r := mux.NewRouter()

    // Serve static pages (i.e. web app)
    r.PathPrefix("/").Handler(http.FileServer(staticDir))

    // Serve User Pages
    createUserRoutes(r)

    return r
}
r.PathPrefix("/").Handler(http.FileServer(staticDir))
路由/user.go

package routes

import (
    "fmt"
    "net/http"

    "github.com/gorilla/mux"
)

func createUserRoutes(r *mux.Router) {
    user := r.PathPrefix("/user/").Subrouter()

    // Create a new user
    user.Path("/new").Methods("PUT").HandlerFunc(newUserHandler)

    // Remove a user
    user.Path("/remove/{username:[a-z][a-z0-9]+}").Methods("DELETE").HandlerFunc(removeUserHandler)

    // Update a user
    user.Path("update/{username:[a-z][a-z0-9]+").Methods("POST").HandlerFunc(updateUserHandler)

    // Get a user (Get user information)
    user.Path("/{username:[a-z][a-z0-9]+").Methods("GET").HandlerFunc(getUserHandler)
}

func newUserHandler(resp http.ResponseWriter, req *http.Request) {
    // Do something that might cause an error        

    if err != nil {
        fmt.Println(err)
        resp.WriteHeader(409)
        resp.Write([]byte(err.Error()))
    } else {
        fmt.Println("Created new user")
        resp.WriteHeader(201)
        resp.Write([]byte("Created new user"))
    }
}

func removeUserHandler(resp http.ResponseWriter, req *http.Request) {
}

func updateUserHandler(resp http.ResponseWriter, req *http.Request) {
}

func getUserHandler(resp http.ResponseWriter, req *http.Request) {
}
每当我向服务器的根路径(即服务于静态内容的路径)发出请求时,服务器都会按预期响应主页。但是,任何其他调用都会导致404响应,我使用cURL测试请求。例如,对的格式错误的请求应返回409,但返回404。如果我希望得到201的回复,也是一样的


看起来一切正常,我已经检查了三次,但我不知道问题出在哪里。

结果表明,解决方案和通常一样简单。这条线在路线上。走吧

r.PathPrefix("/").Handler(http.FileServer(staticDir))
是导致意外路由的原因。使用PathPrefix时,似乎会将所有URL路由到第一个匹配的前缀(在本例中为该前缀)。这就解释了为什么提供静态文件,但其他的都不起作用

解决办法是改用该函数。正如文档中所解释的,有一个细微的区别;如果给定模板是完整URL路径的前缀,则PathPrefix匹配,而path不匹配。因此,上面的一行现在看起来是这样的,以解决我遇到的问题:

r.Path("/").Handler(http.FileServer(staticDir))

gorilla/mux和net/http处理路由的方式不同:谢谢,但我认为这并不能解决我的问题,除非我在那篇文章中遗漏了一些我应该在我的文章中应用的东西。我在提供静态文件方面没有问题。我的问题在于其他HTTP调用,例如创建新用户的PUT请求。对不起。我误读了你说的话。我以为你得到的是静态文件上的404。不用担心!我无论如何都解决了这个问题。如果您感兴趣,请在下面发布我的答案。