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

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

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

&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js&quot;&gt;&lt;/script&gt;
&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;
&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;
&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;
&lt;script type=&quot;text/javascript&quot; src=&quot;/static/scripts/test.js&quot;&lt;/script&gt;
&lt;body&gt;
	&lt;div id=&quot;chart&quot;&gt;&lt;/div&gt;
&lt;/body&gt;

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 := &quot;/&quot; + pathName + &quot;/&quot;
		fmt.Println(pathPrefix)
		fmt.Println(pathValue)
		router.PathPrefix(pathPrefix).Handler(http.StripPrefix(pathPrefix, http.FileServer(http.Dir(pathValue))))
	}


	// router.PathPrefix(&quot;/static/scripts&quot;).Handler(http.FileServer(http.Dir(&quot;./static/scripts/&quot;)))
	return router
}

var staticDirectory = &quot;/static&quot;

var staticPaths = map[string]string{
	&quot;scripts&quot;: staticDirectory + &quot;/scripts/&quot;,
}

var routes = Routes{
	Route{
		&quot;Index&quot;,
		&quot;GET&quot;,
		&quot;/&quot;,
		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= &quot;static/scripts/test.js&quot;
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

请尝试以下代码:

// 创建新的路由器。
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(&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:

确定