go+js登录注册例子(带邮箱验证)
1 搭建服务器
1 package index 2 3 import ( 4 "log" 5 "net/http" 6 7 "2021.6.28_WebServer_email.go/lib/utils" 8 ) 9 10 const ( 11 path = "json/users.json" 12 UserName = "UserName" //Cookie{Name: UserName, Value: email} 13 ) 14 15 var ( 16 uti = utils.NewUtils() //实用包 17 codes = make(map[string]string, 900000) //验证码集合 "email": "code", ... 18 users = make(map[string]string, 900000) //在线用户集合 "email": "1", ... 19 ) 20 21 //运行服务器 22 func RunServer() { 23 24 // 启用静态文本服务 25 http.Handle( 26 "/view/", 27 http.StripPrefix( 28 "/view/", 29 http.FileServer(http.Dir("view")), 30 ), 31 ) 32 33 // 设置路由,js的ajax请求地址与HandleFunc第一个参数对应(url) 34 http.HandleFunc("/", show) //显示主页 35 http.HandleFunc("/register", register) //注册 36 http.HandleFunc("/requestEmail", requestEmail) //请求邮件验证码 37 http.HandleFunc("/login", login) //登录 38 http.HandleFunc("/isLogin", isLogin) //检测用户是否已经登录 39 http.HandleFunc("/exit", exit) //退出登录 40 41 // 启动web服务,监听9090端口(如果没有遇到错误会一直运行下去) 42 //浏览器访问 http://localhost:9090 43 err := http.ListenAndServe(":9090", nil) 44 if err != nil { 45 log.Fatal("ListenAndServe: ", err) 46 } 47 48 }