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 =https://www.cnblogs.com/weihexinCode/p/ [ 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
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/288594.html
標籤:Go
