Httprouter对静态文件的处理有问题。

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

Httprouter trouble with static files

问题

我正在使用一个路由器(httprouter),并希望从根目录下提供静态文件。

css文件位于

static/style.css

在模板中

<link href="./static/style.css" rel="stylesheet">

main.go

router := httprouter.New()
router.ServeFiles("/static/*filepath", http.Dir("/static/"))
router.GET("/", Index)

但是访问 http://localhost:3001/static/style.css 时出现404错误,渲染页面中的样式也无法生效。

英文:

I'm using a router (httprouter) and would like to serve static files from root.

css file in

static/style.css

in template

&lt;link href=&quot;./static/style.css&quot; rel=&quot;stylesheet&quot;&gt;

main.go

router := httprouter.New()
router.ServeFiles(&quot;/static/*filepath&quot;, http.Dir(&quot;/static/&quot;))
router.GET(&quot;/&quot;, Index)

But http://localhost:3001/static/style.css gives me an 404 error and style in render page doesn't work too.

答案1

得分: 6

尝试将http.Dir("/static/")替换为http.Dir("static")(这将是静态目录的相对路径)或http.Dir("/absolute/path/to/static")。只需进行这个单一更改,你提供的示例就可以正常工作。

另请参阅httprouter的ServeFiles文档:

func (r *Router) ServeFiles(path string, root http.FileSystem)

ServeFiles从给定的文件系统根目录中提供文件。路径必须以"/*filepath"结尾,文件将从本地路径/defined/root/dir/*filepath提供。例如,如果根目录是"/etc",*filepath是"passwd",则本地文件"/etc/passwd"将被提供。内部使用http.FileServer,因此使用http.NotFound而不是Router的NotFound处理程序。要使用操作系统的文件系统实现,请使用http.Dir:

router.ServeFiles("/src/*filepath", http.Dir("/var/www"))

这也可能有所帮助-https://stackoverflow.com/questions/24849844/third-party-router-and-static-files

我必须承认,我不太清楚为什么需要两次使用"static"。如果我将http.Dir设置为".",则只需在浏览器中导航到localhost:3001/static/static/style.css即可正常工作。

英文:

Try to replace http.Dir(&quot;/static/&quot;) with http.Dir(&quot;static&quot;) (which will be the relative path to your static dir) or with http.Dir(&quot;/absolute/path/to/static&quot;). Your example with this single change works for me.

Also see httprouter's ServeFiles documentation:

> func (r *Router) ServeFiles(path string, root http.FileSystem)
>
> ServeFiles serves files from the given file system root. The path must end with "/*filepath", files are then served from the local path /defined/root/dir/*filepath. For example if root is "/etc" and *filepath is "passwd", the local file "/etc/passwd" would be served. Internally a http.FileServer is used, therefore http.NotFound is used instead of the Router's NotFound handler. To use the operating system's file system implementation, use http.Dir:
>
> router.ServeFiles("/src/*filepath", http.Dir("/var/www"))

This might also be of help - https://stackoverflow.com/questions/24849844/third-party-router-and-static-files

I must admit that it's unclear to me why 'static' is needed twice. If I set http.Dir to "." it all works with the single difference that I need to navigate to localhost:3001/static/static/style.css

答案2

得分: 2

在调用router.ServeFiles("/static/*filepath", http.Dir("/static/"))时,第二个参数提供了根目录,而第一个参数定义了从该根目录开始的路径。因此,可以尝试使用以下方式:

router.ServeFiles("*filepath", http.Dir("/static"))

而不需要两次提及/static/*

英文:

In call router.ServeFiles(&quot;/static/*filepath&quot;, http.Dir(&quot;/static/&quot;)) second argument provide root and first arg define the path from that root. So , try

router.ServeFiles(&quot;*filepath&quot;, http.Dir(&quot;/static&quot;))

without mentioning /static/ twice.

答案3

得分: 0

这是我让它工作的方式:

func main() {
    router := httprouter.New()
    router.GET("/", Index)
    router.ServeFiles("/static/*filepath", http.Dir("static"))

    log.Fatal(http.ListenAndServe(":3001", router))
}

这段代码使用了httprouter库来创建一个路由器,并定义了一个GET请求的处理函数Index。同时,它还使用ServeFiles函数来处理静态文件请求,并指定了静态文件的目录为"static"。最后,使用http.ListenAndServe函数来监听端口3001并启动服务器。

英文:

This is how I got it working:

func main() {
	router := httprouter.New()
	router.GET(&quot;/&quot;, Index)
	router.ServeFiles(&quot;/static/*filepath&quot;,http.Dir(&quot;static&quot;))

	log.Fatal(http.ListenAndServe(&quot;:3001&quot;, router))
}

huangapple
  • 本文由 发表于 2015年4月25日 08:05:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/29859453.html
匿名

发表评论

匿名网友

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

确定