| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- package main
- import (
- "fmt"
- "github.com/buaazp/fasthttprouter"
- "github.com/valyala/fasthttp"
- "log"
- )
- // index 页
- func Index(ctx *fasthttp.RequestCtx) {
- ctx.Request.Header.Peek("userid")//获取header头参数
- fmt.Fprint(ctx, "Welcome")
- }
- // 简单路由页
- func Hello2(ctx *fasthttp.RequestCtx) {
- name:= ctx.UserValue("name").(string) //获取路由参数name
- fmt.Fprintf(ctx, "hello,%s",name)
- }
- // 获取GET请求json数据
- func TestGet(ctx *fasthttp.RequestCtx) {
- values := ctx.QueryArgs() // 使用 ctx.QueryArgs() 方法
- fmt.Fprint(ctx, string(values.Peek("abc"))) // 不加string返回的byte数组
- fmt.Fprint(ctx, string(ctx.FormValue("abc"))) // 获取表单数据
- }
- // 获取post的请求json数据
- func TestPost(ctx *fasthttp.RequestCtx) {
- postBody := ctx.PostBody() // 这两行可以获取PostBody数据,文件上传也有用
- fmt.Fprint(ctx, string(postBody))
- fmt.Fprint(ctx, string(ctx.FormValue("abc"))) // 获取表单数据
- }
- func main() {
- // 创建路由
- router := fasthttprouter.New()
- // 不同的路由执行不同的处理函数
- router.GET("/", Index)
- router.GET("/hello/:name", Hello2)
- router.GET("/get", TestGet)
- // post方法
- router.POST("/post", TestPost)
- // 启动web服务器,监听 0.0.0.0:80
- log.Fatal(fasthttp.ListenAndServe("0.0.0.0:10087", router.Handler))
- }
|