duanjia7912 2015-09-07 19:29
浏览 57
已采纳

无法使用Golang服务器找到静态脚本

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条回答 默认 最新

  • dongwuchu0314 2015-12-21 13:13
    关注

    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"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置