我寫了一個 golang 網(wǎng)絡服務器,我之前提供靜態(tài)資源,但在改變我的項目結(jié)構后,這不再有效。這是我的項目結(jié)構ProjectFolder/ node_modules/ scripts/ test.es6.js server/ handlers.go main.go routes.go static/ scripts/ test.js test.js.map Gruntfile.js index.html package.json這是我的 index.html<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script><script type="text/javacript" src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.2.1/backbone-min.js"></script><script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script><script type="text/javascript" src="/static/scripts/test.js"</script><body> <div id="chart"></div></body>這是我的routes.gofunc NewRouter() *mux.Router { router := mux.NewRouter().StrictSlash(true) for _, route := range routes { router. Methods(route.Method). Path(route.Pattern). Name(route.Name). Handler(route.HandlerFunc) } for pathName, pathValue := range staticPaths { pathPrefix := "/" + pathName + "/" fmt.Println(pathPrefix) fmt.Println(pathValue) router.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix, http.FileServer(http.Dir(pathValue)))) } // router.PathPrefix("/static/scripts").Handler(http.FileServer(http.Dir("./static/scripts/"))) return router}var staticDirectory = "/static"var staticPaths = map[string]string{ "scripts": staticDirectory + "/scripts/",}var routes = Routes{ Route{ "Index", "GET", "/", Index, },}當我點擊 localhost:8200 時,我在加載 test.js 時得到 404,但 index.html 正在被點擊。以前,這是不使用 http.FileServer 來提供靜態(tài)資源的問題,但我現(xiàn)在正在使用它。我已經(jīng)嘗試過 index.html 中路徑的其他變體src= "static/scripts/test.js"src= "../static/scripts/test.js"到底是怎么回事?
1 回答

撒科打諢
TA貢獻1934條經(jīng)驗 獲得超2個贊
請嘗試以下操作:
// Create new router.
gorillaMux := mux.NewRouter()
// Match /res/ prefix to local /res/ folder.
gorillaMux.PathPrefix("/res/").Handler(http.StripPrefix("/res/", http.FileServer(http.Dir("./res/"))))
這將使http://example.com/res/js/script.js尋找./res/js/script.js
因此,您必須在 HTML 中完全限定您的資源: src= "/static/scripts/test.js"
- 1 回答
- 0 關注
- 173 瀏覽
添加回答
舉報
0/150
提交
取消