Golang Gorilla mux with http.FileServer returning 404

I posted this on golang-nuts discussion group and got this solution from Toni Cárdenas ...

The standard net/http ServeMux (which is the standard handler you are using when you use http.Handle) and the mux Router have different ways of matching an address.

See the differences between http://golang.org/pkg/net/http/#ServeMux and http://godoc.org/github.com/gorilla/mux.

So basically, http.Handle('/images/', ...) matches '/images/whatever', while myRouter.Handle('/images/', ...) only matches '/images/', and if you want to handle '/images/whatever', you have to ...

Option 1 - Use a regular expression match in your router

myRouter.Handle("/images/{rest}",
     http.StripPrefix("/images/", http.FileServer(http.Dir(HomeFolder + "images/"))))

Option 2 - Use the PathPrefix method on your router:

myRouter.PathPrefix("/images/").Handler(http.StripPrefix("/images/", 
     http.FileServer(http.Dir(HomeFolder + "images/"))))

Tags:

Go

Gorilla