Goji SubRouter返回404

时间:2019-03-19 17:18:40

标签: go goji

这是一些代码

package main

import (
    "fmt"
    "net/http"

    "github.com/zenazn/goji"
    "github.com/zenazn/goji/web"
    "github.com/zenazn/goji/web/middleware"
)

type handler struct{}

func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    subMux := web.New()
    subMux.Use(middleware.SubRouter)
    subMux.Post("/:id", func(c web.C, w http.ResponseWriter, r *http.Request) {
        fmt.Fprint(w, "OK")
    })

    subMux.ServeHTTP(w, r)
}

func main() {
    goji.Handle("/inner/*", handler{})
    goji.Serve()
}

围绕此的主要思想是封装处理程序路由并使用标准的net / http Handler接口。那么,为什么以下代码产生404而不使用subrouter?

curl -X POST http://localhost:8000/inner/5
404 page not found

1 个答案:

答案 0 :(得分:0)

如果您这样更改它,则可以获取帖子数据。

package main

import (
    "fmt"
    "net/http"

    "github.com/zenazn/goji"
    "github.com/zenazn/goji/web"
    "github.com/zenazn/goji/web/middleware"
)

type handler struct{}

func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "OK")
}

func main() {
    subMux := web.New()
    subMux.Use(middleware.SubRouter)
    subMux.Post("/:id", handler{})

    goji.Handle("/inner/*", subMux)
    goji.Serve()
}