func main() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/api", home)
fs := http.FileServer(http.Dir("../public"))
http.Handle("/", fs)
http.HandleFunc("/ws", handleConnections)
go handleMessages()
log.Println("http server started on :8000")
err := http.ListenAndServe(":8000", nill)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
使用上面的代码, /api 路由会给出 404。但是如果我将 更改err := http.ListenAndServe(":8000", nill)为err := http.ListenAndServe(":8000", router), /api 路由会起作用,但 / 路由(我服务于前端)会给出 404。
我如何让它们都工作?
编辑:完整代码 - https://codeshare.io/2Kpyb8
万千封印
相关分类