How do you return HTML files as the default HTTP / path while still allowing specific child routes? I would like to serve a static index.html
and some other HTML pages while still having specific routes passed to other http.Handlers
.
In the example below I'm using the httprouter mux, but I'm interested in examples for net/http
as well.
router := httprouter.New()
router.GET("/api", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {...})
router.ServeFiles("/*filepath", http.Dir(projectpath.Join("templates")))
router.GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.FileServer(http.Dir("templates")).ServeHTTP(w, r)
})
router.GET("/api", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {...})
Or using net/http
http.Handle("/", http.FileServer(http.Dir("templates")))
http.Handle("/api", func(w http.ResponseWriter, r *http.Request){...})
http.ListenAndServe(":80", nil)