路由返回404的问题是由Gorilla Mux引起的。

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

Routes returning 404 for mux gorilla

问题

在我的Go应用程序中,我正在使用gorilla/mux。

我希望实现以下功能:

http://host:3000/ 从子目录"frontend"静态地提供文件,并且
http://host:3000/api/ 及其子路径由指定的函数提供。

使用以下代码,两个调用都不起作用。
/index.html 是唯一一个起作用的(但它加载的资源不起作用)。我做错了什么?

package main

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

func main() {
  routineQuit := make(chan int)

  router := mux.NewRouter().StrictSlash(true)
  router.PathPrefix("/").Handler(http.FileServer(http.Dir("./frontend/")))
  router.HandleFunc("/api", Index)
  router.HandleFunc("/api/abc", AbcIndex)
  router.HandleFunc("/api/abc/{id}", AbcShow)
  http.Handle("/", router)
  http.ListenAndServe(":" + strconv.Itoa(3000), router)

  <- routineQuit
}

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

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

func AbcShow(w http.ResponseWriter, r *http.Request) {
  vars := mux.Vars(r)
  todoId := vars["todoId"]
  fmt.Fprintln(w, "Todo show:", todoId)
}
英文:

In my Go application, I'm using gorilla/mux.

I would like to have

http://host:3000/ to be serving files statically from the subdirectory "frontend" and
http://host:3000/api/ and its subpaths being served by the specified functions.

With the following code, neither of the calls work.
/index.html is the only one that doesn (but not the resources being loaded by it). What am I doing wrong?

package main

import (
  &quot;log&quot;
  &quot;net/http&quot;
  &quot;fmt&quot;
  &quot;strconv&quot;
  &quot;github.com/gorilla/mux&quot;
)

func main() {
  routineQuit := make(chan int)

  router := mux.NewRouter().StrictSlash(true)
  router.PathPrefix(&quot;/&quot;).Handler(http.FileServer(http.Dir(&quot;./frontend/&quot;)))
  router.HandleFunc(&quot;/api&quot;, Index)
  router.HandleFunc(&quot;/api/abc&quot;, AbcIndex)
  router.HandleFunc(&quot;/api/abc/{id}&quot;, AbcShow)
  http.Handle(&quot;/&quot;, router)
  http.ListenAndServe(&quot;:&quot; + strconv.Itoa(3000), router)

  &lt;- routineQuit
}

func Abc(w http.ResponseWriter, r *http.Request) {
  fmt.Fprintln(w, &quot;Index!&quot;)
}

func AbcIndex(w http.ResponseWriter, r *http.Request) {
  fmt.Fprintln(w, &quot;Todo Index!&quot;)
}

func AbcShow(w http.ResponseWriter, r *http.Request) {
  vars := mux.Vars(r)
  todoId := vars[&quot;todoId&quot;]
  fmt.Fprintln(w, &quot;Todo show:&quot;, todoId)
}

答案1

得分: 8

Gorilla的mux路由按照它们添加的顺序进行评估。因此,第一个匹配请求的路由将被使用。

在你的情况下,/处理程序将匹配每个传入的请求,然后在frontend/目录中查找文件,最后显示404错误。你只需要交换路由的顺序就可以运行它:

router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/api/abc/{id}", AbcShow)
router.HandleFunc("/api/abc", AbcIndex)
router.HandleFunc("/api", Abc)
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./frontend/")))
http.Handle("/", router)
英文:

Gorilla's mux routes are evaluated in the order in which they are added. Therefore, the first route to match the request is used.

In your case, the / handler will match every incoming request, then look for the file in the frontend/ directory, then display a 404 error. You just need to swap your routes order to get it running:

router := mux.NewRouter().StrictSlash(true)
router.HandleFunc(&quot;/api/abc/{id}&quot;, AbcShow)
router.HandleFunc(&quot;/api/abc&quot;, AbcIndex)
router.HandleFunc(&quot;/api&quot;, Abc)
router.PathPrefix(&quot;/&quot;).Handler(http.FileServer(http.Dir(&quot;./frontend/&quot;)))
http.Handle(&quot;/&quot;, router)

huangapple
  • 本文由 发表于 2015年3月3日 19:54:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/28831190.html
匿名

发表评论

匿名网友

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

确定