亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

FileServer 使用 MIME 類型錯誤而不是 404 處理 not found css

FileServer 使用 MIME 類型錯誤而不是 404 處理 not found css

Go
繁花如伊 2022-06-13 16:17:58
我正在運行一個基本http.FileServer的靜態站點服務,我遇到了一個問題,即對不存在的 css 文件的請求因 MIME 類型錯誤而被取消:Refused to apply style from 'http://localhost:8080/assets/main.css' because its MIME type ('text/plain')理想情況下,我更希望用 404 錯誤來處理它,因為它實際上應該是這樣的。我可以嘗試任何可能的解決方法嗎?
查看完整描述

1 回答

?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

從net/http源代碼(fs.go):


// toHTTPError returns a non-specific HTTP error message and status code

// for a given non-nil error value. It's important that toHTTPError does not

// actually return err.Error(), since msg and httpStatus are returned to users,

// and historically Go's ServeContent always returned just "404 Not Found" for

// all errors. We don't want to start leaking information in error messages.

func toHTTPError(err error) (msg string, httpStatus int) {

    if os.IsNotExist(err) {

        return "404 page not found", StatusNotFound

    }

    if os.IsPermission(err) {

        return "403 Forbidden", StatusForbidden

    }

    // Default:

    return "500 Internal Server Error", StatusInternalServerError

}

文件服務器返回 200 和 404 錯誤的純文本文件。瀏覽器嘗試將此純文本錯誤頁面解釋為 CSS 文件,并引發錯誤。


這種返回純文本文件的行為不能被FileServer().


正如已經指出的那樣,這并不是net/http.


如果由于某種原因您不希望這種行為,您可以探索為 404 響應創建自定義處理程序,這已在此線程中進行了探索。您還可以使用像 Gorilla 這樣的路由庫,它對未找到的頁面具有可覆蓋的行為。


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

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號