Http Go Gorilla mux子计算机和静态文件 问题
您好,我想创建一个web服务器,使用路由器和子计算机显示2个页面和2个静态目录 我无法理解为什么路由器提供的静态目录会显示,而子计算机处理的静态服务器却不工作 下面显示了代码、文件系统方案和网页:所示和所需 文件系统方案 密码 我希望在web服务器中显示的页面 我在web服务器中拥有的页面Http Go Gorilla mux子计算机和静态文件 问题,http,go,gorilla,Http,Go,Gorilla,您好,我想创建一个web服务器,使用路由器和子计算机显示2个页面和2个静态目录 我无法理解为什么路由器提供的静态目录会显示,而子计算机处理的静态服务器却不工作 下面显示了代码、文件系统方案和网页:所示和所需 文件系统方案 密码 我希望在web服务器中显示的页面 我在web服务器中拥有的页面 尝试将子文件服务器行更改为(在StripPrefix调用中包括sub路径) 下面的代码适用于我 package main import ( "net/http" "github.com/g
尝试将子文件服务器行更改为(在StripPrefix调用中包括
sub
路径)
下面的代码适用于我
package main
import (
"net/http"
"github.com/gorilla/mux"
)
func index(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Index"))
}
func main() {
r := mux.NewRouter()
r.Handle("/static", http.StripPrefix("/static", http.FileServer(http.Dir("./"))))
r.HandleFunc("/", index)
sub := r.PathPrefix("/sub").Subrouter()
sub.Handle("/static", http.StripPrefix("/sub/static", http.FileServer(http.Dir("./"))))
sub.HandleFunc("/", index)
http.ListenAndServe(":8080", r)
}
尝试将子文件服务器行更改为(在StripPrefix调用中包括
sub
路径)
下面的代码适用于我
package main
import (
"net/http"
"github.com/gorilla/mux"
)
func index(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Index"))
}
func main() {
r := mux.NewRouter()
r.Handle("/static", http.StripPrefix("/static", http.FileServer(http.Dir("./"))))
r.HandleFunc("/", index)
sub := r.PathPrefix("/sub").Subrouter()
sub.Handle("/static", http.StripPrefix("/sub/static", http.FileServer(http.Dir("./"))))
sub.HandleFunc("/", index)
http.ListenAndServe(":8080", r)
}
http://localhost:8080/ ----> (index)
http://localhost:8080/static ---> (presentation of the file systemfolder)
http://localhost:8080/sub/ ---> (index)
http://localhost:8080/sub/static ---> (presentation of the file system folder)
http://localhost:8080/ ----> (index)
http://localhost:8080/static ---> (presentation of the file system folder)
http://localhost:8080/sub/ ---> (index)
http://localhost:8080/sub/static ---> (404 page not found)
sub.Handle("/static", http.StripPrefix("/sub/static", http.FileServer(http.Dir("./"))))
package main
import (
"net/http"
"github.com/gorilla/mux"
)
func index(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Index"))
}
func main() {
r := mux.NewRouter()
r.Handle("/static", http.StripPrefix("/static", http.FileServer(http.Dir("./"))))
r.HandleFunc("/", index)
sub := r.PathPrefix("/sub").Subrouter()
sub.Handle("/static", http.StripPrefix("/sub/static", http.FileServer(http.Dir("./"))))
sub.HandleFunc("/", index)
http.ListenAndServe(":8080", r)
}