Ошибка MIME-типа (text / plain) в Golang при работе с CSS - PullRequest
0 голосов
/ 23 февраля 2019

Я создаю свой первый веб-проект Go и получаю эту ошибку на консоли браузера при загрузке своей страницы

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.

Я не уверен, что я делаю неправильно, потому что я уже добавил этокод для загрузки статических файлов

http.Handle("/static/",http.StripPrefix("/static/",http.FileServer(http.Dir("static"))))

Вот так выглядит мой файл main.go:

 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()
}

Folder Structure

Редактировать: Решена проблема

Поскольку я использовал httprouter в качестве мультиплексора, я не мог использовать

http.Handle("/static/",http.StripPrefix("/static/",http.FileServer(http.Dir("static"))))

Мне пришлось обновить функцию ServeFiles httprouter и обновить код до router.ServeFiles("/static/*filepath",http.Dir("static"))

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...