返回404为多用途大猩猩的路由 [英] Routes returning 404 for mux gorilla

查看:109
本文介绍了返回404为多用途大猩猩的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想拥有



在我的Go应用程序中,我正在使用gorilla / mux。 > http:// host:3000 / 将从子目录frontend静态提供文件
http:// host:3000 / api / 及其子路径由指定的函数提供。



使用以下代码,电话工作。
/index.html 是唯一一个不会被加载的资源。我做错了什么?

 包主
$ b $导入(
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),路由器)

< - 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)
}


解决方案

大猩猩的多路复用路由按其添加顺序进行评估。因此,使用匹配请求的第一条路径。



在你的情况下, / 处理程序将匹配每个传入的请求,然后在 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)


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 (
  "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)
}

解决方案

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("/api/abc/{id}", AbcShow)
router.HandleFunc("/api/abc", AbcIndex)
router.HandleFunc("/api", Abc)
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./frontend/")))
http.Handle("/", router)

这篇关于返回404为多用途大猩猩的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆