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 }

1.1 当前对 http.HandleFunc("/", show) 的理解

   第一个参数是相对于main.go的相对路径, 例如 http.HandleFunc("/login", login), 在浏览器输入地址 http://localhost:9090/login 就会执行login函数,

  所以第二个函数应该填当路径为http://localhost:9090/login时要执行的函数名

 

1.5 http.HandleFunc("/", show)与ajax的用法

  

 1 "use strict"
 2 
 3 import {TUI} from './lib/Utils.js';//视图包
 4 import {Ajax} from './lib/Ajax.js';//ajax包
 5 import {index} from './index.js';
 6 
 7 const ajax = new Ajax();
 8 
 9 //ui视图的数据
10 const data = [
11     {
12         title: "qq邮箱",
13         valueUrl: ".email",
14     },
15     {
16         title: "密码",
17         ps: "必须6-16位字符",
18         valueUrl: ".password",
19     },
20     {
21         valueUrl: ".send",
22         buttonValue: "send"
23     }
24 ];
25 
26 var o = {
27     type: "请填写表单",
28     email: "",
29     password: "",
30     send: (e, o)=>{
31         ajax.run({
32             url: "/login",
33             data: "email=" + o.email + "&password=" + o.password,
34             success: (d)=>{
35                 if(d === "1") index.getUser();
36                 else alert("登录失败")
37             }
38         });
39     }
40 }
41 
42 //实例化ui
43 var ui = new TUI(o, data).setTitle("登 录");
44 
45 var login = {ui: ui}
46 
47 export {login}
login.js

  ajax的url值应该与第一个参数相对应

 

 

2 显示主页

 1 package index
 2 
 3 import (
 4     "fmt"
 5     "html/template"
 6     "net/http"
 7 )
 8 
 9 //显示主页
10 // w表示response对象,回复客户端信息
11 // r表示客户端请求对象,包含了请求头,请求参数等等
12 func show(w http.ResponseWriter, r *http.Request) {
13 
14     // 回复请求后是否关闭连接
15     r.Close = true
16 
17     //获得一个与template关联的模板
18     t, err := template.ParseFiles("view/index.html")
19     if err != nil {
20         fmt.Fprintf(w, "parse template error: %s", err.Error())
21         return
22     }
23 
24     //应用到第二个参数上,并写入w输出
25     t.Execute(w, nil)
26 
27 }






2.5
进入http://localhost:9090主页时首先会执行show函数
路由器http.HandleFunc("/", show)会给show传递2个实参: w, r


服务器语言: golang

前端语言: html5(推荐使用谷歌浏览器)

通信: ajax & json

项目: 登录注册系统

模拟运行: 运行.exe文件,然后打开谷歌浏览器输入地址: http://localhost:9090

ps: 注册完成的用户保存到了json文件里面

ps: 实现了发送smtp邮箱验证注册邮箱是否可用

ps: 实现了json与go之间的互相转换

ps: 实现了操作json文件

作者qq: 3247940050

时间: 2021.6.28
/*---------------------------三八线-------------------------------------*/

目录介绍:

index: 实现服务器和路由的地方

json: 存放用户信息的json文件

lib: 一些实用包

view: html, css, js 文件(前端视图代码)

 



源码地址: https://pan.baidu.com/s/1TV1j5BeZ7ZhidCq7aQXePA

提取码:   0000

压缩包名: 2021.6.28_WebServer_email.go

















...
posted @ 2021-06-28 03:37  鸡儿er  阅读(434)  评论(0编辑  收藏  举报