I noticed that there are two ways to specify a path in the gorilla/mux
router:
r.PathPrefix("/api").Handler(APIHandler)
And:
r.Handle("/api", APIHandler)
What is the difference?
Also, I don't understand the difference between a router and a route in the context of gorilla/mux
.
PathPrefix()
returns a route, which has a Handler()
method. However, we cannot call Handler()
on a router, we must call Handle()
.
Look at the following example:
r.PathPrefix("/").Handler(http.FileServer(http.Dir(dir+"/public")))
I am trying to serve static files from a public directory. The above expression works without any problem. My HTML and JavaScript are served as expected. However, once I add something to the path, e.g.
r.PathPrefix("/home").Handler(http.FileServer(http.Dir(dir+"/public")))
Then I am getting 404, not found error on localhost:<port>/home
.