如何使路径“/”不匹配 Golang 中所有其他不匹配的路径 net/http
How to make path "/" dont match all others not matched paths in Golang net/http
我正在使用 golang net/http
在 API
中创建一些端点。
我有一个 index
函数映射到 /
路径。我需要任何未明确声明为 mux
至 return 404
.
的 path
docs 说:
Note that since a pattern ending in a slash names a rooted subtree, the pattern "/" matches all paths not matched by other registered patterns, not just the URL with Path == "/".
那么,我该怎么做呢?
关注 MRE:
package main
import (
"fmt"
"log"
"net/http"
)
func index(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(w, "index")
}
func foo(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(w, "foo")
}
func bar(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(w, "bar")
}
func main() {
mux := http.NewServeMux()
s := &http.Server{
Addr: ":8000",
Handler: mux,
}
mux.HandleFunc("/", index)
mux.HandleFunc("/foo", foo)
mux.HandleFunc("/bar", bar)
log.Fatal(s.ListenAndServe())
}
当我运行:
$ curl 'http://localhost:8000/'
index
$ curl 'http://localhost:8000/foo'
foo
$ curl 'http://localhost:8000/bar'
bar
$ curl 'http://localhost:8000/notfoo'
index // Expected 404 page not found
由于您使用的多路复用器会将 /
处理程序与任何未注册的处理程序相匹配,因此您必须在调用 /
路径的处理程序时检查路径:
package main
import (
"fmt"
"log"
"net/http"
)
func index(w http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/" { // Check path here
http.NotFound(w, req)
return
}
fmt.Fprintln(w, "index")
}
func foo(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(w, "foo")
}
func bar(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(w, "bar")
}
func main() {
mux := http.NewServeMux()
s := &http.Server{
Addr: ":8000",
Handler: mux,
}
mux.HandleFunc("/foo", foo)
mux.HandleFunc("/bar", bar)
mux.HandleFunc("/", index)
log.Fatal(s.ListenAndServe())
}
我正在使用 golang net/http
在 API
中创建一些端点。
我有一个 index
函数映射到 /
路径。我需要任何未明确声明为 mux
至 return 404
.
path
docs 说:
Note that since a pattern ending in a slash names a rooted subtree, the pattern "/" matches all paths not matched by other registered patterns, not just the URL with Path == "/".
那么,我该怎么做呢?
关注 MRE:
package main
import (
"fmt"
"log"
"net/http"
)
func index(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(w, "index")
}
func foo(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(w, "foo")
}
func bar(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(w, "bar")
}
func main() {
mux := http.NewServeMux()
s := &http.Server{
Addr: ":8000",
Handler: mux,
}
mux.HandleFunc("/", index)
mux.HandleFunc("/foo", foo)
mux.HandleFunc("/bar", bar)
log.Fatal(s.ListenAndServe())
}
当我运行:
$ curl 'http://localhost:8000/'
index
$ curl 'http://localhost:8000/foo'
foo
$ curl 'http://localhost:8000/bar'
bar
$ curl 'http://localhost:8000/notfoo'
index // Expected 404 page not found
由于您使用的多路复用器会将 /
处理程序与任何未注册的处理程序相匹配,因此您必须在调用 /
路径的处理程序时检查路径:
package main
import (
"fmt"
"log"
"net/http"
)
func index(w http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/" { // Check path here
http.NotFound(w, req)
return
}
fmt.Fprintln(w, "index")
}
func foo(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(w, "foo")
}
func bar(w http.ResponseWriter, req *http.Request) {
fmt.Fprintln(w, "bar")
}
func main() {
mux := http.NewServeMux()
s := &http.Server{
Addr: ":8000",
Handler: mux,
}
mux.HandleFunc("/foo", foo)
mux.HandleFunc("/bar", bar)
mux.HandleFunc("/", index)
log.Fatal(s.ListenAndServe())
}