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

时间:2015-09-07 19:29:31

标签: javascript go

我写了一个golang网络服务器,之前我正在提供静态资源,但在改变我的项目结构之后,它已经不再适用了。

这是我的项目结构

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(&#34; ../ static / scripts&#34;,http.StripPrefix(&#34; ../ static / scripts&#34;,http.FileServer(http.Dir(&#34) ;&#34;))))

但那仍然没有奏效。

0 个答案:

没有答案