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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法加載模塊腳本:需要一個 JavaScript 模塊

無法加載模塊腳本:需要一個 JavaScript 模塊

Go
手掌心 2022-12-19 14:07:26
我使用 vite 作為我的 React 應用程序的構(gòu)建工具,并使用 golang 作為后端。我構(gòu)建了用于生產(chǎn)的應用程序并將該應用程序托管在我的 http 服務器上。我的目錄結(jié)構(gòu):server  |- dist  |    | index.html  |    |- assets  |         | index.js  |         | index.css  | main.go托管我的文件的代碼看起來像(在 main.go 中)fs := http.FileServer(http.Dir("./dist"))http.Handle("/", fs)在 index.html 中<script type="module" crossorigin src="/assets/index.fd457ca0.js"></script><link rel="stylesheet" href="/assets/index.bdcfd918.css">該代碼確實發(fā)送了正確的文件,但標頭錯誤。
查看完整描述

1 回答

?
智慧大石

TA貢獻1946條經(jīng)驗 獲得超3個贊

所以我不得不編寫自己的文件服務器來手動設置標頭,例如:


contentTypeMap := map[string]string{

    ".html": "text/html",

    ".css":  "text/css",

    ".js":   "application/javascript",

}


filepath.Walk("./dist", func(path string, info os.FileInfo, err error) error {

    if err != nil {

        log.Fatalf(err.Error())

    }

    if info.IsDir() {

        return err

    }


    dirPath := filepath.ToSlash(filepath.Dir(path))

    contentType := contentTypeMap[filepath.Ext(info.Name())]

    handlePath := "/" + strings.Join(strings.Split(dirPath, "/")[1:], "/")


    hf := func(w http.ResponseWriter, r *http.Request) {

        w.Header().Add("Content-Type", contentType) // <---- key part

        http.ServeFile(w, r, path)

    }


    if handlePath != "/" {

        handlePath += "/" + info.Name()

    }


    mainRouter.HandleFunc(handlePath, hf)

    return nil

})

(如果代碼不好,請優(yōu)化,我自己做了解決方案,我嘗試了很多東西來滿足我的需要)


現(xiàn)在,我收到了帶有正確標頭的正確文件。


而且我找不到任何解決方案來處理http.FileServer在 http 包中使用的自定義標頭。如果存在,請?zhí)峁┮粋€簡單的解決方案。


查看完整回答
反對 回復 2022-12-19
  • 1 回答
  • 0 關注
  • 175 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號