Serving an angular dist folder with go

I'm simply trying to serve an angular application's dist folder using go. I tried an approach which worked well for me with react. e.g.

spa := http.StripPrefix("/", http.FileServer(http.Dir("path/to/dist")))
// m is a gorilla mux router.
m.PathPrefix("/").Handler(spa)

However when I navigate to the port I'm serving on I receive a 404 error.

1 answer

  • answered 2018-07-11 07:06 kars7e

    When using relative paths you have to be extra cautious about the working directory of your application. Try printing the value returned by os.Getwd and see if it makes sense when combined with "path/to/dist".

    Also, since http.FileServer checks if path starts with /, and if it doesn't it adds it back, using http.StripPrefix with "/" is not needed.