无法在 Golang 服务器中找到静态脚本。

huangapple go评论112阅读模式
英文:

Cannot locate static scripts with golang server

问题

我写了一个 golang 的 web 服务器,在改变项目结构后,之前用来提供静态资源的功能不再起作用。

这是我的项目结构:

  1. ProjectFolder/
  2. node_modules/
  3. scripts/
  4. test.es6.js
  5. server/
  6. handlers.go
  7. main.go
  8. routes.go
  9. static/
  10. scripts/
  11. test.js
  12. test.js.map
  13. Gruntfile.js
  14. index.html
  15. package.json

这是我的 index.html:

  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
  2. <script type="text/javacript" src="//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.2.1/backbone-min.js"></script>
  3. <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
  4. <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
  5. <script type="text/javascript" src="/static/scripts/test.js"></script>
  6. <body>
  7. <div id="chart"></div>
  8. </body>

这是我的 routes.go:

  1. func NewRouter() *mux.Router {
  2. router := mux.NewRouter().StrictSlash(true)
  3. for _, route := range routes {
  4. router.
  5. Methods(route.Method).
  6. Path(route.Pattern).
  7. Name(route.Name).
  8. Handler(route.HandlerFunc)
  9. }
  10. for pathName, pathValue := range staticPaths {
  11. pathPrefix := "/" + pathName + "/"
  12. fmt.Println(pathPrefix)
  13. fmt.Println(pathValue)
  14. router.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix, http.FileServer(http.Dir(pathValue))))
  15. }
  16. // router.PathPrefix("/static/scripts").Handler(http.FileServer(http.Dir("./static/scripts/")))
  17. return router
  18. }
  19. var staticDirectory = "/static"
  20. var staticPaths = map[string]string{
  21. "scripts": staticDirectory + "/scripts/",
  22. }
  23. var routes = Routes{
  24. Route{
  25. "Index",
  26. "GET",
  27. "/",
  28. Index,
  29. },
  30. }

当我访问 localhost:8200 时,加载 test.js 时出现 404 错误,但是 index.html 是可以访问的。

之前,这个问题是因为没有使用 http.FileServer 来提供静态资源,但是现在我正在使用它。

我尝试了 index.html 中路径的其他变化:

  1. src="static/scripts/test.js"
  2. 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

  1. ProjectFolder/
  2. node_modules/
  3. scripts/
  4. test.es6.js
  5. server/
  6. handlers.go
  7. main.go
  8. routes.go
  9. static/
  10. scripts/
  11. test.js
  12. test.js.map
  13. Gruntfile.js
  14. index.html
  15. package.json

This is my index.html

  1. &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js&quot;&gt;&lt;/script&gt;
  2. &lt;script type=&quot;text/javacript&quot; src=&quot;//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.2.1/backbone-min.js&quot;&gt;&lt;/script&gt;
  3. &lt;script type=&quot;text/javascript&quot; src=&quot;//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js&quot;&gt;&lt;/script&gt;
  4. &lt;script type=&quot;text/javascript&quot; src=&quot;https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js&quot;&gt;&lt;/script&gt;
  5. &lt;script type=&quot;text/javascript&quot; src=&quot;/static/scripts/test.js&quot;&lt;/script&gt;
  6. &lt;body&gt;
  7. &lt;div id=&quot;chart&quot;&gt;&lt;/div&gt;
  8. &lt;/body&gt;

This is my routes.go

  1. func NewRouter() *mux.Router {
  2. router := mux.NewRouter().StrictSlash(true)
  3. for _, route := range routes {
  4. router.
  5. Methods(route.Method).
  6. Path(route.Pattern).
  7. Name(route.Name).
  8. Handler(route.HandlerFunc)
  9. }
  10. for pathName, pathValue := range staticPaths {
  11. pathPrefix := &quot;/&quot; + pathName + &quot;/&quot;
  12. fmt.Println(pathPrefix)
  13. fmt.Println(pathValue)
  14. router.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix, http.FileServer(http.Dir(pathValue))))
  15. }
  16. // router.PathPrefix(&quot;/static/scripts&quot;).Handler(http.FileServer(http.Dir(&quot;./static/scripts/&quot;)))
  17. return router
  18. }
  19. var staticDirectory = &quot;/static&quot;
  20. var staticPaths = map[string]string{
  21. &quot;scripts&quot;: staticDirectory + &quot;/scripts/&quot;,
  22. }
  23. var routes = Routes{
  24. Route{
  25. &quot;Index&quot;,
  26. &quot;GET&quot;,
  27. &quot;/&quot;,
  28. Index,
  29. },
  30. }

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

  1. src= &quot;static/scripts/test.js&quot;
  2. src= &quot;../static/scripts/test.js&quot;

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

请尝试以下代码:

  1. // 创建新的路由器。
  2. gorillaMux := mux.NewRouter()
  3. // 将/res/前缀匹配到本地的/res/文件夹。
  4. gorillaMux.PathPrefix("/res/").Handler(http.StripPrefix("/res/", http.FileServer(http.Dir("./res/"))))
  5. 这将使`http://example.com/res/js/script.js`查找`./res/js/script.js`
  6. 因此HTML您必须完全指定资源的路径`src="/static/scripts/test.js"`
英文:

Try the following:

  1. // Create new router.
  2. gorillaMux := mux.NewRouter()
  3. // Match /res/ prefix to local /res/ folder.
  4. gorillaMux.PathPrefix(&quot;/res/&quot;).Handler(http.StripPrefix(&quot;/res/&quot;, http.FileServer(http.Dir(&quot;./res/&quot;))))

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= &quot;/static/scripts/test.js&quot;

huangapple
  • 本文由 发表于 2015年9月8日 03:29:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/32444993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定