我寫(xiě)了一個(gè) golang 網(wǎng)絡(luò)服務(wù)器,我之前提供靜態(tài)資源,但在改變我的項(xiàng)目結(jié)構(gòu)后,這不再有效。這是我的項(xiàng)目結(jié)構(gòu)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, },}當(dāng)我點(diǎn)擊 localhost:8200 時(shí),我在加載 test.js 時(shí)得到 404,但 index.html 正在被點(diǎn)擊。以前,這是不使用 http.FileServer 來(lái)提供靜態(tài)資源的問(wèn)題,但我現(xiàn)在正在使用它。我已經(jīng)嘗試過(guò) index.html 中路徑的其他變體src= "static/scripts/test.js"src= "../static/scripts/test.js"到底是怎么回事?
無(wú)法使用 golang 服務(wù)器定位靜態(tài)腳本
繁華開(kāi)滿(mǎn)天機(jī)
2021-11-08 10:25:14