第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

無(wú)法在 Go webapp 中提供外部 CSS 文件

無(wú)法在 Go webapp 中提供外部 CSS 文件

我最近開(kāi)始學(xué)習(xí)圍棋,我想開(kāi)發(fā)一個(gè)簡(jiǎn)單的網(wǎng)站。但是我不知道如何為這個(gè)網(wǎng)站使用外部 CSS 文件。這是我的目錄結(jié)構(gòu):./  main.go  static/    css/      home.css  templates/    home.html這是我的 main.go 文件:package mainimport (        "html/template"        "log"        "net/http")func main() {        http.HandleFunc("/", homeHandler)        fs := http.FileServer(http.Dir("static/"))        http.Handle("/static/", http.StripPrefix("/static/", fs))        log.Println("Listening on :8080...")        log.Fatal(http.ListenAndServe(":8080", nil))}func homeHandler(w http.ResponseWriter, r *http.Request) {        renderTemplate(w, "home")}func renderTemplate(w http.ResponseWriter, tmpl string) {        t, err := template.ParseFiles("templates/" + tmpl + ".html")        if err != nil {                http.Error(w, err.Error(), http.StatusInternalServerError)                return        }        err = t.Execute(w, nil)        if err != nil {                http.Error(w, err.Error(), http.StatusInternalServerError)        }}在 home.html 中,我在標(biāo)題中添加了這個(gè):<link rel="stylesheet" href="/css/home.css">在瀏覽器中調(diào)試時(shí),似乎找到了該文件,但 MIME 類(lèi)型出現(xiàn)錯(cuò)誤:瀏覽器控制臺(tái)屏幕截圖我認(rèn)為這兩行可以解決這個(gè)問(wèn)題,但顯然它沒(méi)有:fs := http.FileServer(http.Dir("static/"))http.Handle("/static/", http.StripPrefix("/static/", fs))有人知道我做錯(cuò)了什么嗎?
查看完整描述

1 回答

?
藍(lán)山帝景

TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個(gè)贊

您必須正確設(shè)置響應(yīng)中的 Content-Type 標(biāo)頭。如果沒(méi)有內(nèi)容類(lèi)型,大多數(shù)瀏覽器將不會(huì)執(zhí)行 css。像下面這樣的東西應(yīng)該可以工作,但這本質(zhì)上只是一個(gè)草圖:


 http.HandleFunc("/static/",func(wr http.ResponseWriter,req *http.Request) {

    // Determine mime type based on the URL

    if req.URL.Path.HasSuffix(".css") {

      wr.Header().Set("Content-Type","text/css")

    } 

    http.StripPrefix("/static/", fs)).ServeHTTP(wr,req)

 })


查看完整回答
反對(duì) 回復(fù) 2022-06-06
  • 1 回答
  • 0 關(guān)注
  • 129 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)