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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀