欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

go+js登录注册例子(带邮箱验证)

程序员文章站 2022-06-16 19:53:58
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 ......

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的用法

  

go+js登录注册例子(带邮箱验证)
 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}

  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


go+js登录注册例子(带邮箱验证)

服务器语言: 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

















...