英文:
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 (
"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)
}
答案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("/api/abc/{id}", AbcShow)
router.HandleFunc("/api/abc", AbcIndex)
router.HandleFunc("/api", Abc)
router.PathPrefix("/").Handler(http.FileServer(http.Dir("./frontend/")))
http.Handle("/", router)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论