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

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

Go http.FileServer gives unexpected 404 error

问题

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

package main

import (
	"log"
	"net/http"
)

func main() {
	srv := http.NewServeMux()

	// 文件服务器1
	uiServer := http.FileServer(http.Dir("./ui"))
	srv.Handle("/", uiServer)

	// 文件服务器2
	staticFilesServer := http.FileServer(http.Dir("./files"))
	srv.Handle("/files", staticFilesServer)

	if err := http.ListenAndServe(":8080", srv); err != nil {
		log.Fatal(err)
	}
}

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


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

package main

import (
	"log"
	"net/http"
)

func main() {
	srv := http.NewServeMux()

	staticFilesServer := http.FileServer(http.Dir("./files"))
	srv.Handle("/files", staticFilesServer)

	if err := http.ListenAndServe(":8080", srv); err != nil {
		log.Fatal(err)
	}
}

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


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


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

|- main.go
|- ui
|--- index.html
|- files
|--- file1.txt
|--- file2.csv
|--- 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:

package main

import (
	"log"
	"net/http"
)

func main() {
	srv := http.NewServeMux()

	// File server 1
	uiServer := http.FileServer(http.Dir("./ui"))
	srv.Handle("/", uiServer)

	// File server 2
	staticFilesServer := http.FileServer(http.Dir("./files"))
	srv.Handle("/files", staticFilesServer)

	if err := http.ListenAndServe(":8080", srv); err != nil {
		log.Fatal(err)
	}
}

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:

package main

import (
	"log"
	"net/http"
)

func main() {
	srv := http.NewServeMux()

	staticFilesServer := http.FileServer(http.Dir("./files"))
	srv.Handle("/files", staticFilesServer)

	if err := http.ListenAndServe(":8080", srv); err != nil {
		log.Fatal(err)
	}
}

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:

|- main.go
|- ui
|--- index.html
|- files
|--- file1.txt
|--- file2.csv
|--- file3.img

答案1

得分: 3

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

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:

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:

确定