- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有点困惑。许多示例显示了两者的用法:http.ServeFile(..)
和 http.FileServer(..)
,但似乎它们具有非常接近的功能。我也没有找到有关如何设置自定义 NotFound 处理程序的信息。
// This works and strip "/static/" fragment from path
fs := http.FileServer(http.Dir("static"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
// This works too, but "/static2/" fragment remains and need to be striped manually
http.HandleFunc("/static2/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, r.URL.Path[1:])
})
http.ListenAndServe(":8080", nil)
我尝试阅读源代码,它们都使用 serveFile(ResponseWriter, *Request, FileSystem, string, bool)
底层函数。然而 http.FileServer
返回 fileHandler
并带有自己的 ServeHTTP()
方法,并在提供文件之前做一些准备工作(例如 path.Clean()) .
那么为什么需要这种分离呢?哪种方法更好用?以及如何设置自定义 NotFound 处理程序,例如在未找到请求的文件时?
最佳答案
主要区别在于 http.FileServer
有效地将 HTTP 前缀与文件系统进行了几乎 1:1 的映射。用简单的英语,它提供了一个完整的目录路径。及其所有的 child 。
假设您有一个名为 /home/bob/static
的目录,并且您进行了以下设置:
fs := http.FileServer(http.Dir("/home/bob/static"))
http.Handle("/static/", http.StripPrefix("/static", fs))
您的服务器会接受请求,例如/static/foo/bar
并提供 /home/bob/static/foo/bar
(或 404)中的任何内容
相比之下,ServeFile
是一个较低级别的帮助器,可用于实现类似于 FileServer 的东西,或者实现你自己的路径修改,以及任何数量的东西。它只是获取命名的本地文件并通过 HTTP 连接发送它。就其本身而言,它不会提供整个目录前缀(除非您编写了一个处理程序来进行类似于 FileServer 的一些查找)
注意天真地为文件系统提供服务是一件有潜在危险的事情(有潜在的方法可以打破根树)因此我建议除非你真的知道什么您正在这样做,使用 http.FileServer
和 http.Dir
因为它们包含检查以确保人们无法突破 FS,其中 ServeFile
没有。
附录不幸的是,你的第二个问题,你如何做一个自定义的 NotFound 处理程序,不容易回答。因为正如您所注意到的,这是从内部函数 serveFile
调用的,所以没有 super 容易进入的地方。可能有一些偷偷摸摸的事情,比如用你自己的 ResponseWriter
拦截响应,它会拦截 404 响应代码,但我会把这个练习留给你。
关于戈朗。用什么? http.ServeFile(..) 还是 http.FileServer(..)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28793619/
我想知道将任何数据发送到模板(html/template 包)的真正方法是什么?我的代码如下: var templates = template.Must(template.ParseFiles(
我有密码 if config.Process.TraceLog != "" { f, err := os.OpenFile(config.Process.TraceLog, os.O_RDWR
大家好。我是 GO 的新手,需要一些帮助。我有一个具有这种结构的项目 但让我们假设我的逻辑包 中有 100500 个项目。如果我只需要为特定包导入一个或两个文件怎么办?我可以这样做还是只能导入完整包?
package main import ( "fmt" ) func main() { var square int box := [4]int{1, -2, 3, 4}
我想在每个范围循环后递增一个变量。但是,似乎无法使用标准(for init; condition; post {})语法,因此我想知道为什么。这是我正在尝试做的事情 for item := r
这个问题在这里已经有了答案: Creating a Constant Type and Restricting the Type's Values (2 个答案) 关闭 6 年前。 什么是只允许一组
我试过编译我的简单程序: func main(){fmt.Printf("Hello")} 根据文档在我的 PC 上使用 64 位 Debian Linux 到 MIPS 架构 https://gol
为什么不在 channel 上发送并阻止执行?我怎样才能使这个星座工作,以便我可以向 MoneyDive() 发送信号并继续执行? package main import ( "fmt" )
这是来自 Golang.org http://golang.org/pkg/sort/ // By is the type of a "less" function that defines the
class type User { name string } 是否可以在运行时添加字段age? 最佳答案 不,这是不可能的。 即使使用 Java 或 C++ 语法代替有效的 Go 也不行。
当我编译下面的程序时 func myPrint(v ...interface{}) { fmt.Println("Hello", v...) } func main() { m
有一个问题,这里发生了什么? forever := make(chan bool) log.Printf(" [*] Waiting for messages. To exit press CTRL+
当我运行我的 GoLang 程序时,它抛出了这个异常: packages/go-lang/1.3/pkg/tool/linux_amd64/6l: readsym 不同步 它是否来 self 的 am
我时常会遇到诸如零垃圾或有效使用内存等概念。例如在知名包的特性部分 httprouter您可以看到以下内容: Zero Garbage: The matching and dispatching pr
最近读了golang规范,遇到了一些有趣的操作符: & bitwise AND integers | bitwise OR integers
有这个包https://github.com/open-telemetry/opentelemetry-proto其中仅包含 protobuf 定义。要生成 golang 代码,必须输入: make
有这个包https://github.com/open-telemetry/opentelemetry-proto其中仅包含 protobuf 定义。要生成 golang 代码,必须输入: make
我正在使用下面的代码连接到 grpc 服务器,clientConn 对象用于所有后续的 rpc 调用。 maxDelay 设置为 5 秒。现在由于服务器出现问题,它没有响应 grpc 调用。所以我的客
初始数据: rawdata := []int{17, 23, 100500} 结果: result := convert(rawdata) expected := "1723100500" 我应该用
我想用我的智能手机操作我的窗帘。现在每次我改变手机的位置时都有一个方法 func main() { OnUpdate(func(tPos int) { wc(tPos,cPos)
我是一名优秀的程序员,十分优秀!