3

My file structure is so main.go is inside folder co and inside folder cmd.

-assets/
-cmd/co/main.go

Here is my code in main.go,

func main() {
    router := NewRouter()
    router.HandleFunc("/search", api.SearchHandler)
    router.HandleFunc("/product-details/{author}/{name}/{id}//", api.DetailsHandler)
    // ... listenAndServe
}

func NewRouter() *mux.Router {
    router := mux.NewRouter()
    staticDir := "/assets/"
    router.
        PathPrefix(staticDir).
        Handler(http.StripPrefix(staticDir, http.FileServer(http.Dir("."+staticDir))))
    return router
}

Handle /search which is static url works perfectly but when I go to product-details/{author} ... (dynamic url path) with subroutes an error 404 is sent to me by web browser. So go is not serving folder in dynamic path in this manner

  • What router are you using? The stdlib mux doesn't support dynamic routes. – Adrian Mar 25 '21 at 17:39
  • Please show the *actual, full* path of the request that is failing. For us to know whether there is a problem with the server or not we need to know that the input is in fact valid. The string `product-details/{author}` is not a valid request path but an incomplete pattern. When asking a question try to provide as clear and accurate info as possible, that way you have a better chance of getting an accurate answer. – mkopriva Mar 25 '21 at 18:46

0 Answers0