I am building my first Go web project and I am getting this error on the browser console when I load my page
Refused to apply style from 'http://localhost:8080/static/css/processor-auth.css' because its MIME type ('text/plain') is not a supported stylesheet MIME type, and strict MIME checking is enabled.
I am not sure what I am doing wrong because I have already added this code to load the static files
http.Handle("/static/",http.StripPrefix("/static/",http.FileServer(http.Dir("static"))))
This is how my main.go file looks like:
package main
import(
"net/http"
"os"
"html/template"
"github.com/julienschmidt/httprouter"
)
// Auth struct handler
type auth struct{}
func (auth *auth) ServeHTTP(w http.ResponseWriter, r *http.Request){
wd,_:= os.Getwd()
t := template.Must(template.ParseFiles(wd+"/templates/processor/auth.html"))
err:=t.Execute(w,nil)
if err !=nil{
http.Error(w,"Could not execute template",500)
}
}
func main(){
router:= httprouter.New()
// set the static files
http.Handle("/static/",http.StripPrefix("/static/",http.FileServer(http.Dir("static"))))
router.Handler("GET","/auth",&auth{})
server := http.Server{
Addr:"127.0.0.1:8080",
Handler:router,
}
server.ListenAndServe()
}
Edit: Solved the issue
Since I was using httprouter
as my multiplexer I could not use
http.Handle("/static/",http.StripPrefix("/static/",http.FileServer(http.Dir("static"))))
I had to update to the httprouter's ServeFiles function and update the code to
router.ServeFiles("/static/*filepath",http.Dir("static"))