【问题标题】:Gorilla Mux routes not resolvingGorilla Mux 路由无法解析
【发布时间】:2016-04-26 22:41:33
【问题描述】:

所以,我正在使用 Go 和 Gorilla Mux 开发一个简单的 RESTful API。我的第二条路线无法正常工作时遇到问题,它返回 404 错误。我不确定问题是什么,因为我是 Go 和 Gorilla 的新手。我敢肯定这很简单,但我似乎找不到它。我认为这可能是我使用不同的自定义包这一事实的问题。

这个问题很相似,Routes returning 404 for mux gorilla,但接受的解决方案并没有解决我的问题

我的代码如下所示:

Router.go:

package router

import (
    "github.com/gorilla/mux"
    "net/http"
)

type Route struct {
    Name        string
    Method      string
    Pattern     string
    HandlerFunc http.HandlerFunc
}

type Routes []Route

func NewRouter() *mux.Router {

router := mux.NewRouter().StrictSlash(true)
    for _, route := range routes {
        router.
        Methods(route.Method).
        Path(route.Pattern).
        Name(route.Name).
        Handler(route.HandlerFunc)
    }

    return router
}

var routes = Routes{
    Route{
        "CandidateList",
        "GET",
        "/candidate",
        CandidateList,
    },
    Route{
        "Index",
        "GET",
        "/",
        Index,
    },
}

Handlers.go

package router

import (
    "fmt"
    "net/http"
)

func Index(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Welcome!")
}

func CandidateList(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "CandidateList!")
}

Main.go

package main

import (
    "./router"
    "log"
    "net/http"
)

func main() {
    rout := router.NewRouter()
    log.Fatal(http.ListenAndServe(":8080", rout))
}

只访问 localhost:8080 会返回 Welcome!但转到 localhost:8080/candidate 会返回 404 Page Not Found 错误。我感谢任何输入和帮助!谢谢!

这是我的 Router.go 文件的更新版本,仍然出现同样的问题。

Router.go

package router

import (
    "github.com/gorilla/mux"
    "net/http"
)

type Route struct {
    Method      string
    Pattern     string
    HandlerFunc http.HandlerFunc
}

type Routes []Route

func NewRouter() *mux.Router {

    router := mux.NewRouter().StrictSlash(true)
    for _, route := range routes {
        router.
            Methods(route.Method).
            Path(route.Pattern).
            Handler(route.HandlerFunc).GetError()
    }

    return router
}

var routes = Routes{
   Route{
      "GET",
      "/candidate",
      CandidateList,
   },
   Route{
       "GET",
       "/",
       Index,
    },
}

【问题讨论】:

  • 我能够重现这一点,现在它正在使用 OP 发布的相同代码。尝试使用GetError() 来查看注册路线是否会给您带来任何问题。比如:err := router.Methods(route.Method).Path(route.Pattern).Name(route.Name).Handler(route.HandlerFunc).GetError()
  • 添加 .GetError() 并没有返回任何与我目前看到的不同的东西。
  • 奇怪...我做的最后一件事是在注册路由时删除 .Name() 函数调用。尝试这样做并确保您没有缓存响应。
  • 删除了 .Name() 调用和 Route 中的相应字段,但仍然没有运气,我已将更新后的 Router.go 文件添加到问题中。
  • 哦..确保保存返回的错误:err := router.Methods()...GetError() 并检查它是否返回错误if err != nil{ log.Println(err) }

标签: go gorilla


【解决方案1】:

看来我的项目在主 src 目录中保留了旧版本的 Router.go 和 Handlers.go 文件。通过删除这些重复文件并使用 go run Main.go 重新运行 Main.go,我能够获得要识别的路线。

【讨论】:

  • 不要使用相对导入。始终通过完整路径导入以避免这种情况和其他问题。
猜你喜欢
  • 2014-09-08
  • 2016-04-07
  • 1970-01-01
  • 2015-04-05
  • 2015-04-18
  • 2014-12-16
  • 2021-06-05
  • 2017-11-07
  • 1970-01-01
相关资源
最近更新 更多