从邮递员检查时获取 404 页面未找到错误
Getting 404 page not found error while checking from postman
我 运行 下面的代码使用 goapp serve
。从邮递员检查时不知何故出现 404 page not found
错误。你能帮我解决这个问题吗
package hello
import (
"fmt"
"net/http"
"github.com/julienschmidt/httprouter"
)
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Welcome!\n")
}
func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}
func init() {
router := httprouter.New()
router.GET("/", Index)
router.GET("/hello/:name", Hello)
//log.Fatal(http.ListenAndServe(":8080", router))
}
在邮递员传递端点
http://localhost:8080/hello/hyderabad
扩展我上面的评论:
处理程序函数(或来自 julienschmidt/httprouter
的路由器)不会自行注册。相反,它需要向 http 服务器注册。
最简单的方法通常是使用以下方式注册默认的 ServeMux:http.Handle("/", router)
因此,将 init 函数更改为以下内容将起作用:
func init() {
router := httprouter.New()
router.GET("/", Index)
router.GET("/hello/:name", Hello)
http.Handle("/", router)
}
我 运行 下面的代码使用 goapp serve
。从邮递员检查时不知何故出现 404 page not found
错误。你能帮我解决这个问题吗
package hello
import (
"fmt"
"net/http"
"github.com/julienschmidt/httprouter"
)
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Welcome!\n")
}
func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}
func init() {
router := httprouter.New()
router.GET("/", Index)
router.GET("/hello/:name", Hello)
//log.Fatal(http.ListenAndServe(":8080", router))
}
在邮递员传递端点
http://localhost:8080/hello/hyderabad
扩展我上面的评论:
处理程序函数(或来自 julienschmidt/httprouter
的路由器)不会自行注册。相反,它需要向 http 服务器注册。
最简单的方法通常是使用以下方式注册默认的 ServeMux:http.Handle("/", router)
因此,将 init 函数更改为以下内容将起作用:
func init() {
router := httprouter.New()
router.GET("/", Index)
router.GET("/hello/:name", Hello)
http.Handle("/", router)
}