英文:
Cannot locate static scripts with golang server
问题
我写了一个 golang 的 web 服务器,在改变项目结构后,之前用来提供静态资源的功能不再起作用。
这是我的项目结构:
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.go:
func 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 来提供静态资源,但是现在我正在使用它。
我尝试了 index.html 中路径的其他变化:
src="static/scripts/test.js"
src="../static/scripts/test.js"
发生了什么?
编辑-
我简化了一切,尝试做这个:
router.Handle("../static/scripts", http.StripPrefix("../static/scripts", http.FileServer(http.Dir("."))))
但是仍然不起作用。
英文:
I wrote a golang webserver and I was serving static resources before, but after changing my project structure, that no longer works.
This is my project structure
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
This is my 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>
This is my routes.go
func 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,
},
}
When I hit localhost:8200, I get a 404 when loading test.js, but index.html is getting hit.
Before, this was an issue with not using http.FileServer to serve static resources, but I am using it now.
I've tried other variations of the path in index.html
src= "static/scripts/test.js"
src= "../static/scripts/test.js"
What is going on?
EDIT-
I've simplified everything and tried to do this
router.Handle("../static/scripts", http.StripPrefix("../static/scripts", http.FileServer(http.Dir("."))))
But that still isn't working.
答案1
得分: 1
请尝试以下代码:
// 创建新的路由器。
gorillaMux := mux.NewRouter()
// 将/res/前缀匹配到本地的/res/文件夹。
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"`
英文:
Try the following:
// 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/"))))
This will make http://example.com/res/js/script.js
look for ./res/js/script.js
So, you have to fully qualify you resources in your HTML: src= "/static/scripts/test.js"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论