我用net/http写了一个文件服务器,代码很简单,可以运行,代码如下:
package main
import "net/http"
func main(){
http.Handle("/", http.FileServer(http.Dir("../template")))
http.ListenAndServe(":8888", nil)
}
然后在template目录下的任何文件和目录在ie中都可以访问
然后我用net/http+github.com/gorilla/mux改写了上述代码,如下:
package main
import "net/http"
import "github.com/gorilla/mux"
func main(){
r := mux.NewRouter()
r.Handle("/", http.FileServer(http.Dir("../template")))
http.ListenAndServe(":8888", nil)
}
然后在ie浏览器中访问template目录下的文件和目录,就不行了,请高手看看,这是为何?
``` go
muxRouter := mux.NewRouter()
muxRouter.HandleFunc("/", router.IndexHandleFunc)
serveMux := http.DefaultServeMux
serveMux.Handle("/", muxRouter)
serveMux.HandleFunc("/statics/", router.StaticsHandleFunc)
httpServer := http.Server{
Addr: ":12345",
Handler: serveMux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
httpServer.ListenAndServe()
```
#2
更多评论