gpt4 book ai didi

Goji 子路由器返回 404

转载 作者:数据小太阳 更新时间:2023-10-29 03:20:12 25 4
gpt4 key购买 nike

代码如下

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 处理程序接口(interface)。那么为什么下面的代码会产生 404 而没有使用 subrouter 呢?

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

最佳答案

如果你这样改变它,你可以获得帖子数据。

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()
}

关于Goji 子路由器返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55246659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com