将http.NewServeMux放入http.NewServeMux [英] Put http.NewServeMux into http.NewServeMux

查看:99
本文介绍了将http.NewServeMux放入http.NewServeMux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码.我想将mux1作为子路由器放入mux.Handle中.接下来,我运行代码.我可以访问路径/index,但是不能访问路径/index/sub1.我不知道为什么我可以访问/index但不能访问/index/sub1?

This is my code. I want to put mux1 as sub router into mux.Handle. Next, I run the code. I can visit path /index but I cannot visit path /index/sub1. I don't know why I can visit /index but I cannot visit /index/sub1?

package main

import (
    "io"
    "net/http"
)

func main() {
    mux1 := http.NewServeMux()
    mux1.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        io.WriteString(w, "sub index")
    })
    mux1.HandleFunc("/sub1", func(w http.ResponseWriter, r *http.Request) {
        io.WriteString(w, "sub 1")
    })

    mux := http.NewServeMux()
    mux.Handle("/index", mux1)

    http.ListenAndServe(":8000", mux)
}

推荐答案

您的示例不起作用,因为您使用/index 路径注册了外部"处理程序,该处理程序是一个路径(因为它不以斜杠/结尾)和不是根的子树(即,所有以/index/开头的路径).记录在 http.ServeMux :

Your example doesn't work, because you used the /index path to register the "outer" handler, which is a single path (because it does not end with a slash /) and not a rooted subtree (that is, all paths starting with /index/). This is documented at http.ServeMux:

模式命名固定的,有根的路径(例如"/favicon.ico")或有根的子树(例如"/images/")(请注意末尾的斜杠).较长的模式优先于较短的模式,因此,如果同时为"/images/"和"/images/thumbnails/"注册了处理程序,则将为从"/images/thumbnails/"开始的路径调用后一个处理程序将在"/images/"子树中接收对其他任何路径的请求.

Patterns name fixed, rooted paths, like "/favicon.ico", or rooted subtrees, like "/images/" (note the trailing slash). Longer patterns take precedence over shorter ones, so that if there are handlers registered for both "/images/" and "/images/thumbnails/", the latter handler will be called for paths beginning "/images/thumbnails/" and the former will receive requests for any other paths in the "/images/" subtree.

并且还因为子路由器"不会看到用于注册处理程序的路径,如"/" "/sub1" ,而是完整路径,即"/index/" "/index/sub1" .

And also because the "subrouter" will not see a path like "/" or "/sub1" which you used to register handlers, but the full path, that is: "/index/" and "/index/sub1".

因此,主路由器应该做的是剥离" "/index" 前缀,这是其注册到的路径(不带斜杠).幸运的是,标准库为此提供了一个现成的解决方案: http.StripPrefix() .

So what the main router should do is "strip" the "/index" prefix which is the path it was registered to (without the trailing slash). Fortunately, the standard lib has a ready-solution for this: http.StripPrefix().

因此可行的解决方案:

mux1 := http.NewServeMux()
mux1.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "sub index")
})
mux1.HandleFunc("/sub1", func(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "sub 1")
})

mux := http.NewServeMux()
mux.Handle("/index/", http.StripPrefix("/index", mux1))

http.ListenAndServe(":8000", mux)

测试:

  • URL: http://localhost:8000/index/
    响应:子索引

URL: http://localhost:8000/index/sub1
响应: sub 1

这篇关于将http.NewServeMux放入http.NewServeMux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆