首页 > Web开发 > 详细

9.5 处理http 请求

时间:2018-03-27 01:00:39      阅读:259      评论:0      收藏:0      [点我收藏+]
package main

import (
    "fmt"
    "net/http"
)

func main() {

    mux := http.NewServeMux()
    mux.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) {
        if r.Method == http.MethodGet {
            fmt.Fprintln(w, "User GET")
        }
        if r.Method == http.MethodPost {
            fmt.Fprintln(w, "User POST")
        }
    })

    // separate handler
    itemMux := http.NewServeMux()
    itemMux.HandleFunc("/items/clothes", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintln(w, "Clothes")
    })
    mux.Handle("/items/", itemMux)

    // Admin handlers
    adminMux := http.NewServeMux()
    adminMux.HandleFunc("/ports", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintln(w, "Ports")
    })

    mux.Handle("/admin/",
        http.StripPrefix("/admin", adminMux))

    // Default server
    http.ListenAndServe(":8080", mux)

}

/*
(sx3.5.3) ?  ~ curl -XI http://127.0.0.1:8080/user
(sx3.5.3) ?  ~ curl -XI http://127.0.0.1:8080/userA
404 page not found
(sx3.5.3) ?  ~ curl -X POST http://127.0.0.1:8080/user
User POST

(sx3.5.3) ?  ~ curl  http://127.0.0.1:8080/admin
<a href="/admin/">Moved Permanently</a>.
*/

9.5 处理http 请求

原文:https://www.cnblogs.com/zrdpy/p/8654883.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!