Go的http.FileServer给出了意外的404错误。

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

Go http.FileServer gives unexpected 404 error

问题

我正在尝试运行两个文件服务器,其中一个服务器在ui文件夹中提供index.html,另一个服务器提供其他静态文件,代码如下:

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. )
  6. func main() {
  7. srv := http.NewServeMux()
  8. // 文件服务器1
  9. uiServer := http.FileServer(http.Dir("./ui"))
  10. srv.Handle("/", uiServer)
  11. // 文件服务器2
  12. staticFilesServer := http.FileServer(http.Dir("./files"))
  13. srv.Handle("/files", staticFilesServer)
  14. if err := http.ListenAndServe(":8080", srv); err != nil {
  15. log.Fatal(err)
  16. }
  17. }

两个文件服务器对象的定义方式完全相同,第一个(uiServer)可以正常工作,但第二个(位于localhost:8080/files上的staticFilesServer)会返回404错误。


我缩小了问题的范围,并删除了第一个(正常工作的文件服务器),代码如下:

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. )
  6. func main() {
  7. srv := http.NewServeMux()
  8. staticFilesServer := http.FileServer(http.Dir("./files"))
  9. srv.Handle("/files", staticFilesServer)
  10. if err := http.ListenAndServe(":8080", srv); err != nil {
  11. log.Fatal(err)
  12. }
  13. }

但是,访问路径localhost:8080/files仍然返回404错误。


如果我将处理路径从/files更改为/,它将按预期工作,但这不是我想要的,我只想知道是否可以在除了/之外的路径上提供服务,以及如何实现。


另外,我的文件夹结构如下:

  1. |- main.go
  2. |- ui
  3. |--- index.html
  4. |- files
  5. |--- file1.txt
  6. |--- file2.csv
  7. |--- file3.img
英文:

I'm trying to run two file servers, one of them serving index.html in the ui folder, and one other serving some other static files, like the code below:

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. )
  6. func main() {
  7. srv := http.NewServeMux()
  8. // File server 1
  9. uiServer := http.FileServer(http.Dir("./ui"))
  10. srv.Handle("/", uiServer)
  11. // File server 2
  12. staticFilesServer := http.FileServer(http.Dir("./files"))
  13. srv.Handle("/files", staticFilesServer)
  14. if err := http.ListenAndServe(":8080", srv); err != nil {
  15. log.Fatal(err)
  16. }
  17. }

Both fileServer objects are defined in the exact same way, and the first one (uiServer) works fine, but the second (staticFilesServer on localhost:8080/files), gives me 404.


I narrowed down the problem and removed the first one (working file server), just like the code below:

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. )
  6. func main() {
  7. srv := http.NewServeMux()
  8. staticFilesServer := http.FileServer(http.Dir("./files"))
  9. srv.Handle("/files", staticFilesServer)
  10. if err := http.ListenAndServe(":8080", srv); err != nil {
  11. log.Fatal(err)
  12. }
  13. }

But it still gives me 404 on the path localhost:8080/files


If I change handle path from /files to /, it works as expected, but that's not what i want, I just want to know is it even possible to serve on paths other than / and how can I achieve that.


Also, my folder structure:

  1. |- main.go
  2. |- ui
  3. |--- index.html
  4. |- files
  5. |--- file1.txt
  6. |--- file2.csv
  7. |--- file3.img

答案1

得分: 3

我意识到http.Dir()http.ServeMux.Handle()之间存在关联,它们实际上将它们的路径相加,如下所示:

  1. srv.Handle("/files/", http.FileServer(http.Dir(".")))

上面的代码将服务于./files文件夹中的所有内容,而不是.(如Dir(".")中所写)。
这解决了我的问题。

英文:

I realized that http.Dir() and http.ServeMux.Handle() would have a relation, they actually sum up their paths, as show below:

  1. srv.Handle("/files/", http.FileServer(http.Dir("."))

Code above serves everything inside ./files folder, not . (as written in Dir("."))
And it fixed my problem.

huangapple
  • 本文由 发表于 2022年12月31日 21:26:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/74969821.html
匿名

发表评论

匿名网友

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

确定