DDR爱好者之家 Design By 杰米
要求
- 了解Express框架
- 了解node.js常用模块:express,body-parser,mysql
- 了解express Router
- 了解HTML form表单
- 了解MySQL Server 及安装使用
- 了解SQLyog使用
环境
- OS: Win10
- Node.js: v12.19.0
- Express: v4.17.1
- Yarn: v1.22.10
- 使用VScode IDE
- body-parser:1.19.0
- mysql: 2.18.1
- MySQL Server:5.7
- SQLyog:V12.9
准备
首先在Win10上安装Mysql,一路Next就行。安装完成使用SQLyog连接MySQL Server。连接成功需要创建数据库和数据表
Schema:
CREATE TABLE user ( username char(20) NOT NULL, password char(20) NOT NULL, email char(30) DEFAULT NULL, address char(20) DEFAULT NULL, phonenumber char(20) DEFAULT NULL, logintime int(20) DEFAULT NULL, id int(20) NOT NULL AUTO_INCREMENT, PRIMARY KEY (id), KEY username (username) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
实战
前端
3个page, login.html, register.html.
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登陆注册</title> <link rel="stylesheet" type="text/css" href="/stylesheets/style.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" /> </head> <body> <a href="./register.html" rel="external nofollow" >注册</a> <a href="./login.html" rel="external nofollow" >登录</a> </body> </head> </html>
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登陆注册</title> <link rel="stylesheet" type="text/css" href="/stylesheets/style.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" /> </head> <body> <form action="/login" method="GET" > <label for="">账号:</label> <input name="user" type="text" placeholder="请输入账号"> <br> <label for="">密码:</label> <input type="password" name="password" placeholder="请输入密码"> <br> <input type="submit" value="登录"> </form> </body> </head> </html>
register.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登陆注册</title> <link rel="stylesheet" type="text/css" href="/stylesheets/style.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" /> </head> <body> <form action="/register" method="POST"> <label for="">账号:</label> <input name="user" type="text" placeholder="请输入账号"> <br> <label for="">密码:</label> <input name="psw" type="password" placeholder="请输入密码"> <br> <label for="">重复密码:</label> <input name="pswa" type="password" placeholder="请重复密码"> <br> <input type="submit" value="注册"> </form> </body> </head> </html>
后端 server.js
var express = require("express"); var bodyParser = require("body-parser"); var router = require("./routers"); var app = express(); app.use(bodyParser.urlencoded({ extended: true })); app.use(express.static('public')); app.use('/', router); module.exports = app;
Router
router/index.js, 调用封装好的数据库接口:queryUer, addUser
const express=require("express"); const dao = require("../dao/db"); const router=express.Router(); router.get("/login", function(req,res){ console.dir(req.query); try{ dao.queryUser({username:req.query.user},function(err,record){ if(err){ console.log(err); throw error; } console.log(record); if(record && record.password == req.query.password){ res.send(`${req.query.user}:登陆成功`); }else{ res.send(`${req.query.user}:登陆失败,检查登陆信息是否正确`); } }); } catch(error){ console.log(error); res.send(`${req.body.user}: 登陆失败`); } }) router.post("/register", function(req,res){ console.dir(req.body); try{ if(req.body.psw == req.body.pswa){ dao.addUser({username:req.body.user,password:req.body.psw}); res.send(`${req.body.user}: 注册成功`); } else { console.log(error); res.send(`${req.body.user}: 注册失败:,检查登陆信息是否正确`); } } catch(error){ console.log(error); res.send(`${req.body.user}: 注册失败`); } }) module.exports = router;
数据库接口db.js
dao/db.js
var mysqlClient= require("./mysql"); function addUser (userInfo,callabck){ console.log("addUser:"+ userInfo); var sql= `insert into user(username,password) values('${userInfo.username}','${userInfo.password}')`; console.log("sql:"+ sql); mysqlClient(sql,function(err,rows){ if(err){ console.log("err:"+err); callabck(err,null); } else{ console.log("addUser result:"); console.log(rows); callabck(null,rows); } }) } function queryUser (userInfo,callabck){ console.log("queryUser:"+ userInfo); var sql= `select * from user where username='${userInfo.username}'`; console.log("sql:"+ sql); mysqlClient(sql, function(err,rows){ if(err){ console.log("err:"+err); callabck(err,null); } else{ rows && rows.length>0 "htmlcode">const mysql = require("mysql"); const pool = mysql.createPool({ host:"localhost", user:"root", password:"*****", database:"test" }); function query(sql,callback){ pool.getConnection(function(err,connection){ if(err){ callback(err,null); return } connection.query(sql, function (err,rows) { callback(err,rows); connection.release(); }); }); } module.exports = query;mysql module
yarn add mysql运行index.js
cd src/ && node index.js结果及演示
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年05月18日
2024年05月18日
- 刘月宁《扬琴独奏-木兰辞变奏曲》[FLAC+CUE]
- 陈奕迅《48首选》[WAV分轨][1.8G]
- 林忆莲《陪着我走》 SACD DSD64[DSF][1.1G]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 刘欢范琳琳.1988-信天游迪斯科(TP版)【内蒙古音像】【WAV+CUE】
- 群星-真声音真音乐[WAV+CUE]
- 群星-《茶缘》(TeaPredestinedRelationship)[FLAC]
- RCA白头佬美国头版-斯托科夫斯基[狂想曲][WAV+CUE]
- 【ABC唱片】《春花秋月SACD-062》-WAV分轨
- [ABC唱片]-《俄罗斯三角琴》[K2-064][K2CD]WAV+CUE
- 群星《2007香港高级视听展原音精选SACD》[ISO][2.9G]
- 群星《华纳至尊金曲》2CD[FLAC][865M]
- 周杰伦《叶惠美》[WAV分轨][3.6G]
- 滚石群星.2000-超级BANDBANDBAND【滚石】【WAV+CUE】
- 群星.2016-民谣地图·拾众而歌【众乐纪】【FLAC分轨】