diff --git "a/\350\265\226\350\231\271\351\234\226/20220322cookei/config/db.js" "b/\350\265\226\350\231\271\351\234\226/20220322cookei/config/db.js" new file mode 100644 index 0000000000000000000000000000000000000000..e1b143475ee09e95e6de0637d7af3fac6d05c55e --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220322cookei/config/db.js" @@ -0,0 +1,18 @@ +let dbconfig = { + + host: '127.0.0.1', + user: 'root', + password: '123456', + database: 'yiqing', + port: '3306' + +} +module.exports = dbconfig; + +//dbconfig.key(); + +// json 非常重要!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +// 其实它很单纯,看到 { 开头 }结尾就是json ,它是 key val 形式 ,key 就是 类型数组的键名,value 是对应的值 + +// value 不限定类型 \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220322cookei/home/.keep" "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220322cookei/home/controller/.keep" "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/controller/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220322cookei/home/controller/BaseController.js" "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/controller/BaseController.js" new file mode 100644 index 0000000000000000000000000000000000000000..66d265e4dbefd72703b5274ae8b6b275183c87ad --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/controller/BaseController.js" @@ -0,0 +1,20 @@ + +class BaseController { + + constructor(req, res, nunjucks) { + + this.req = req; + this.res = res; + this.nunjucks = nunjucks; + this.nunjucks.configure('./home/html', { autoescape: true });//配置模板的目录 + + } + + display(path,data){ + return this.nunjucks.render(path,data); + } + + +} + +module.exports = BaseController; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220322cookei/home/controller/IndexController.js" "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/controller/IndexController.js" new file mode 100644 index 0000000000000000000000000000000000000000..57d9af51225f4934757ba33f17176b678f6958aa --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/controller/IndexController.js" @@ -0,0 +1,119 @@ +const BaseController = require("./BaseController"); +let mysql = require("../../model/Base"); + +class IndexController extends BaseController { + + + async index() { + + let mysqlObj = new mysql(); + let date = this.req.query['date']; + //console.log('date-----'+date); + let sql = 'select * from number where date=?'; + //console.log(res); + try { + let result = await mysqlObj.query(sql, date);//等待数据的返回,但是线程可以干别的事情,线程我们就理解为饭店的服务员, + //node只能请得起一个服务员, await + promise 表示,这一段代码需要同步(服务员招待了客户, + //客户告诉服务器,我自己来,你忙别的) 如果说这块是阻塞的,那请求2次,5*2 =10 + //如果不是阻塞的 + //result 是啥类型?数组 + return this.display('index.html', { data: result,title:'我是index11111111' }) + } catch (err) { + console.log(err); + } + + } + + async detail() { + + let mysqlObj = new mysql(); + let date = this.req.query['date']; + //console.log('date-----'+date); + let sql = 'select * from number where date=?'; + //console.log(res); + try { + let result = await mysqlObj.query(sql, date);//等待数据的返回,但是线程可以干别的事情,线程我们就理解为饭店的服务员, + //node只能请得起一个服务员, await + promise 表示,这一段代码需要同步(服务员招待了客户, + //客户告诉服务器,我自己来,你忙别的) 如果说这块是阻塞的,那请求2次,5*2 =10 + //如果不是阻塞的 + //result 是啥类型?数组 + return this.display('detail.html', { data: result }) + } catch (err) { + console.log(err); + } + + } + + + diji() { + + console.log("盖地基"); + + } + + zhuzi() { + + console.log("盖柱子"); + + } + + qizhuang() { + + console.log("砌砖"); + + } + + quchunishuijiang() { + console.log(this.nishuijiang); + + } + + + +} +module.exports = IndexController; + +// let obj = new IndexController('李四');//开始去搭积木,比方说我想搭建个小房子 ,new 就表示 具体要做个工程了 +// // 类是不限制构建次数 +// obj.diji(); +// obj.zhuzi(); +// obj.qizhuang(); +// obj.quchunishuijiang(); + +// let obj2 = new IndexController('张三');//我们每次项目对象正常我们目的是不一样的 +// //this就表示每次构建的这个对象 +// obj2.quchunishuijiang(); + +// console.log(obj==obj2) + + +/** + * 回顾 class java es6 + * + * 面向对象编程三大特点:继承,封装,多态(方法的重写) + * + * 封装: + * 继承:extends 需要继承 + * + * + * + * + * class 中文翻译过来 班级 在编程里面叫 类,我们就按班级理解 + * + * 班级里有很多成员,单元可以有一个,元素 在js 里面就是 函数(js class 里面不写function 就是函数名与 {} , + * + * 在class 里面就不叫函数了,就叫 方法),和属性() + * + * class 的使用 , new 加一个class 名称,我们可以理解为, class 就是一堆的积木,里面有 圆柱体,有长方体.....(就是方法),但是我们积木 + * + * 没有构建成具体的建筑. + * + * + * class :元素 + * 1 函数,不用function 去标识,而且在类里面不叫函数了,叫方法 + * 2 属性 就理解成变量,跟普通变量相对多个 this + * + * + * + */ + diff --git "a/\350\265\226\350\231\271\351\234\226/20220322cookei/home/controller/UserController.js" "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/controller/UserController.js" new file mode 100644 index 0000000000000000000000000000000000000000..fca55a7c31fc85ab085fc7558d5da380a8edf9a1 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/controller/UserController.js" @@ -0,0 +1,47 @@ +const BaseController = require("./BaseController"); +const UserModel = require("../../model/UserModel"); +const EncAndDec = require("../../untis/EecAndDec"); +let mysql=require("mysql") +class UserController extends BaseController { + async login() { + return this.display("login.html"); + } + async userReg(){ + + return this.display("userReg.html"); + } + async handleReg(){ + + + let name = this.req.post['name']; + let password =EncAndDec.md5(this.req.post['password']); + let sql="INSERT into user(name,password)VALUES('"+name+"' ,'"+password+"')"; + let userModel = new UserModel(); + let userList=await userModel.query(sql,[name,password]); + let userInfo={}; + if(userList.length>0){ + userInfo = userList[0]; + this.res.session = JSON.stringify(userInfo); + } + console.log(name); + console.log(password) + return this.display("login.html"); + } + async logins(){ + + return this.display("dlu.html"); + } + + async userList(){ + let user = new UserModel(); + try{ + let userList = await user.select(); + return JSON.stringify(userList); + + }catch(err){ + console.log(err); + } + } + +} +module.exports = UserController; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220322cookei/home/controller/index.js" "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/controller/index.js" new file mode 100644 index 0000000000000000000000000000000000000000..fb69ae86016b681a654ce9e296d5e5e183c54bbd --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/controller/index.js" @@ -0,0 +1,79 @@ +/** + * 在mvc 中,controller 是指挥者者,他指挥着数据获取与模板的渲染 + * + */ + let fs = require("fs"); +let index={ + + + index:async function(req,res){ + let mysql = require("../../model/base"); + let date = req.query['date']; + //console.log('date-----'+date); + let sql = 'select * from number where date=?'; + //console.log(res); + let result= await mysql.query(sql,date,res);//等待数据的返回,但是线程可以干别的事情,线程我们就理解为饭店的服务员, + //node只能请得起一个服务员, await + promise 表示,这一段代码需要同步(服务员招待了客户, + //客户告诉服务器,我自己来,你忙别的) 如果说这块是阻塞的,那请求2次,5*2 =10 + //如果不是阻塞的 + + console.log(result); + let html=''; + for (let val in result) { + html += '' + html += '' + result[val].city + "" + html += '' + result[val].nums + "" + html += '' + + } + + fs.readFile("./home/html/index.html", function (err, data) { + console.log(err); + let datas = data.toString(); + let formatHtml = datas.replace("{{result}}", html); + res.write(formatHtml); + res.end(); + + }) + + + }, + + show: async function(req,res){ + + let mysql = require("../../model/base"); + let date = req.query['date']; + //console.log('date-----'+date); + let sql = 'select * from number order by id desc limit 1 ';//取出最先的一条数据 + //console.log(res); + let result= await mysql.query(sql,date,res);//等待数据的返回,但是线程可以干别的事情,线程我们就理解为饭店的服务员, + //node只能请得起一个服务员, await + promise 表示,这一段代码需要同步(服务员招待了客户, + //客户告诉服务器,我自己来,你忙别的) 如果说这块是阻塞的,那请求2次,5*2 =10 + //如果不是阻塞的 + + console.log(result); + let html=''; + for (let val in result) { + html += '' + html += '' + result[val].city + "" + html += '' + result[val].nums + "" + html += '' + + } + + fs.readFile("./home/html/index.html", function (err, data) { + console.log(err); + let datas = data.toString(); + let formatHtml = datas.replace("{{result}}", html); + res.write(formatHtml); + res.end(); + + }) + + + } +} + +module.exports=index; + +// \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/.keep" "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/detail.html" "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/detail.html" new file mode 100644 index 0000000000000000000000000000000000000000..f6fbfa950367b48ccdd8dcc5e64b93c866cd2c53 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/detail.html" @@ -0,0 +1,9 @@ +{% include='header.html' %} +

各城市疫情

+ {% for item in data %} + + {{ item.city }} + {{ item.nums }} + + {% endfor %} +{% include='footer.html' %} \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/dlu.html" "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/dlu.html" new file mode 100644 index 0000000000000000000000000000000000000000..72d0259cce84ed43935a26ec4ca48b8c33f28841 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/dlu.html" @@ -0,0 +1,12 @@ + + + + + + Document + + + 获取14号数据 + 获取15号数据 + + \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/footer.html" "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/footer.html" new file mode 100644 index 0000000000000000000000000000000000000000..d619210ac5cfa2dd8f909c0a035f7a6128f9c6b0 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/footer.html" @@ -0,0 +1,5 @@ + + + \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/header.html" "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/header.html" new file mode 100644 index 0000000000000000000000000000000000000000..349ba5b9820c1d4ece42fd6fcdd457553b90c183 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/header.html" @@ -0,0 +1,10 @@ + + + + + + + + {{title}} + + diff --git "a/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/index.html" "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/index.html" new file mode 100644 index 0000000000000000000000000000000000000000..d5451621b464070fcf157d390e059b992e1f89a0 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/index.html" @@ -0,0 +1,10 @@ +{% include 'header.html' %} + +

各城市疫情

+{% for item in data %} + + {{ item.city }} + {{ item.nums }} + +{% endfor %} +{% include 'footer.html' %} \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/login.html" "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/login.html" new file mode 100644 index 0000000000000000000000000000000000000000..91a7a097413d6ed41e7518a2e657545b91b122ff --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/login.html" @@ -0,0 +1,16 @@ + + + + + + + Document + + +
+ + + +
+ + \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/userReg.html" "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/userReg.html" new file mode 100644 index 0000000000000000000000000000000000000000..ccd016e524733d38c6d288f5d4368cbc200c0a6d --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220322cookei/home/html/userReg.html" @@ -0,0 +1,21 @@ + + + + + + + 注册于登陆 + + +
+
+ 用户名:
+ 密 码:
+ 确认密码:
+ +
+ +
+ + + \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220322cookei/model/.keep" "b/\350\265\226\350\231\271\351\234\226/20220322cookei/model/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220322cookei/model/Base.js" "b/\350\265\226\350\231\271\351\234\226/20220322cookei/model/Base.js" new file mode 100644 index 0000000000000000000000000000000000000000..937befcfe89b8956fe17e0c8286574e16469edca --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220322cookei/model/Base.js" @@ -0,0 +1,60 @@ + + +let mysqlModule = require("mysql"); + +let db = require("../config/db"); + +class mysql { + + /** + * 初始化 + */ + constructor() { + + } + + + query(sql, value) { + + let promise = new Promise(function (resolve, reject) { + let connection = mysqlModule.createConnection({ host: db.host, user: db.user, password: db.password, database: db.database }); + + connection.connect(); + /** + * 推荐使用占位符方式查询,占位符可以防止 sql注入 占了 80%漏洞都是sql + */ + connection.query(sql, value, function (err, result) { + + if (err) { + reject(err.message) + } else { + + //setTimeout(function () {//模拟数据的慢查询 + resolve(result); + //},5000) + + } + + connection.end(); + }) + + + }) + return promise; + + } + + /** + * 简单的数据查询 + * @param {}} condtion + */ + select() { + + let sql = "select * from " + this.table; + return this.query(sql, '') + } + + +} + +module.exports = mysql; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220322cookei/model/UserModel.js" "b/\350\265\226\350\231\271\351\234\226/20220322cookei/model/UserModel.js" new file mode 100644 index 0000000000000000000000000000000000000000..f5b5c67efca3f38c5003c5d62a44877509c04256 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220322cookei/model/UserModel.js" @@ -0,0 +1,20 @@ + +let Base = require("./Base"); + +class UserModel extends Base{ + + constructor(){ + super();//父亲构造器,意思说 父亲的构造器也要执行 + this.table = 'user'; + + } + + + + + + + +} + +module.exports = UserModel; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220322cookei/untis/EecAndDec.js" "b/\350\265\226\350\231\271\351\234\226/20220322cookei/untis/EecAndDec.js" new file mode 100644 index 0000000000000000000000000000000000000000..e5fa3450dbbb5c71ba545fbde06d99a9d31e8140 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220322cookei/untis/EecAndDec.js" @@ -0,0 +1,15 @@ +let crypto = require("crypto"); + +let EncAndDec = { + + md5: function (data) { + let md5 = crypto.createHash("md5"); + + md5.update(data); + + return md5.digest("hex"); + + } +} + +module.exports=EncAndDec; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220322cookei/url\345\212\250\346\200\201\350\265\204\346\272\220.js" "b/\350\265\226\350\231\271\351\234\226/20220322cookei/url\345\212\250\346\200\201\350\265\204\346\272\220.js" new file mode 100644 index 0000000000000000000000000000000000000000..70ee5314563a9c222a0837062ff50523f5118089 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220322cookei/url\345\212\250\346\200\201\350\265\204\346\272\220.js" @@ -0,0 +1,83 @@ +// 登入页优化,新建注册页面,实现新增用户,用户密码用md5保存 +let http = require("http"); +let fs = require("fs"); +let nunjucks = require("nunjucks"); +let server = http.createServer(); +server.listen(8080); +console.log("进人登录页面 登录成功获取数据"+" http://localhost:8080/?a=login&c=user"); +console.log("注册页面 注册成功进入登陆页面"+ "http://localhost:8080/?a=userReg&c=user"); +server.on('request', function (req, res) { + let postData = ''; + req.on('data', function (chunk) { + postData += chunk; + }); + req.on('end', async function () { + // console.log("流方式获取数据:::::"); + console.log(postData); + let postArr = postData.split("&");// ['name=demo','password=23']; + let postVals = []; + for (let ky in postArr) { + let val = postArr[ky];//'a=index'....... + let temPostArray = val.split("="); + postVals[temPostArray[0]] = temPostArray[1]; + } + req.post = postVals; + let url = req.url; + if (url.lastIndexOf('.') > -1) { + console.log(url); + let path = '.' + url; + readFileByPromise(path, res); + } else { + res.setHeader("Content-type", "text/html;charset=utf8"); + let queryString = url.split("?")[1];//a=index&c=index + let queryArr = queryString.split("&");// ['a=index','c=index']; + let keyVals = []; + for (let key in queryArr) { + let vals = queryArr[key];//'a=index'....... + let temArray = vals.split("="); + keyVals[temArray[0]] = temArray[1]; + } + + req.query = keyVals; + console.log(keyVals['c']); + let controllerPath = './home/controller/' + keyVals['c'].replace(keyVals[0], keyVals['c'][0].toUpperCase()) + "Controller"; + let controller = require(controllerPath); + console.log(controller); + let action = keyVals['a']; + let obj = new controller(req, res, nunjucks); + let content = await obj[action](); + res.setHeader('Set-Cookie',['demo=123456','demo2=78910']); + res.write(content); + res.end(); + + } + }) + + + +}) + + +let promise = function (filename) { + + return new Promise(function (resolve, reject) { + + fs.exists(filename, function (result) { + if (!result) { + filename = './home/imgs/404.png'; + } + fs.readFile(filename, function (err, data) { + resolve(data); + }) + }) + }) +} + +async function readFileByPromise(fileName, res) { + let data = await promise(fileName); + if (data) { + res.write(data); + } + res.end(); +} + diff --git "a/\350\265\226\350\231\271\351\234\226/20220323cookie_session/config/db.js" "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/config/db.js" new file mode 100644 index 0000000000000000000000000000000000000000..e1b143475ee09e95e6de0637d7af3fac6d05c55e --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/config/db.js" @@ -0,0 +1,18 @@ +let dbconfig = { + + host: '127.0.0.1', + user: 'root', + password: '123456', + database: 'yiqing', + port: '3306' + +} +module.exports = dbconfig; + +//dbconfig.key(); + +// json 非常重要!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +// 其实它很单纯,看到 { 开头 }结尾就是json ,它是 key val 形式 ,key 就是 类型数组的键名,value 是对应的值 + +// value 不限定类型 \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/.keep" "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/controller/.keep" "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/controller/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/controller/BaseController.js" "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/controller/BaseController.js" new file mode 100644 index 0000000000000000000000000000000000000000..66d265e4dbefd72703b5274ae8b6b275183c87ad --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/controller/BaseController.js" @@ -0,0 +1,20 @@ + +class BaseController { + + constructor(req, res, nunjucks) { + + this.req = req; + this.res = res; + this.nunjucks = nunjucks; + this.nunjucks.configure('./home/html', { autoescape: true });//配置模板的目录 + + } + + display(path,data){ + return this.nunjucks.render(path,data); + } + + +} + +module.exports = BaseController; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/controller/IndexController.js" "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/controller/IndexController.js" new file mode 100644 index 0000000000000000000000000000000000000000..57d9af51225f4934757ba33f17176b678f6958aa --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/controller/IndexController.js" @@ -0,0 +1,119 @@ +const BaseController = require("./BaseController"); +let mysql = require("../../model/Base"); + +class IndexController extends BaseController { + + + async index() { + + let mysqlObj = new mysql(); + let date = this.req.query['date']; + //console.log('date-----'+date); + let sql = 'select * from number where date=?'; + //console.log(res); + try { + let result = await mysqlObj.query(sql, date);//等待数据的返回,但是线程可以干别的事情,线程我们就理解为饭店的服务员, + //node只能请得起一个服务员, await + promise 表示,这一段代码需要同步(服务员招待了客户, + //客户告诉服务器,我自己来,你忙别的) 如果说这块是阻塞的,那请求2次,5*2 =10 + //如果不是阻塞的 + //result 是啥类型?数组 + return this.display('index.html', { data: result,title:'我是index11111111' }) + } catch (err) { + console.log(err); + } + + } + + async detail() { + + let mysqlObj = new mysql(); + let date = this.req.query['date']; + //console.log('date-----'+date); + let sql = 'select * from number where date=?'; + //console.log(res); + try { + let result = await mysqlObj.query(sql, date);//等待数据的返回,但是线程可以干别的事情,线程我们就理解为饭店的服务员, + //node只能请得起一个服务员, await + promise 表示,这一段代码需要同步(服务员招待了客户, + //客户告诉服务器,我自己来,你忙别的) 如果说这块是阻塞的,那请求2次,5*2 =10 + //如果不是阻塞的 + //result 是啥类型?数组 + return this.display('detail.html', { data: result }) + } catch (err) { + console.log(err); + } + + } + + + diji() { + + console.log("盖地基"); + + } + + zhuzi() { + + console.log("盖柱子"); + + } + + qizhuang() { + + console.log("砌砖"); + + } + + quchunishuijiang() { + console.log(this.nishuijiang); + + } + + + +} +module.exports = IndexController; + +// let obj = new IndexController('李四');//开始去搭积木,比方说我想搭建个小房子 ,new 就表示 具体要做个工程了 +// // 类是不限制构建次数 +// obj.diji(); +// obj.zhuzi(); +// obj.qizhuang(); +// obj.quchunishuijiang(); + +// let obj2 = new IndexController('张三');//我们每次项目对象正常我们目的是不一样的 +// //this就表示每次构建的这个对象 +// obj2.quchunishuijiang(); + +// console.log(obj==obj2) + + +/** + * 回顾 class java es6 + * + * 面向对象编程三大特点:继承,封装,多态(方法的重写) + * + * 封装: + * 继承:extends 需要继承 + * + * + * + * + * class 中文翻译过来 班级 在编程里面叫 类,我们就按班级理解 + * + * 班级里有很多成员,单元可以有一个,元素 在js 里面就是 函数(js class 里面不写function 就是函数名与 {} , + * + * 在class 里面就不叫函数了,就叫 方法),和属性() + * + * class 的使用 , new 加一个class 名称,我们可以理解为, class 就是一堆的积木,里面有 圆柱体,有长方体.....(就是方法),但是我们积木 + * + * 没有构建成具体的建筑. + * + * + * class :元素 + * 1 函数,不用function 去标识,而且在类里面不叫函数了,叫方法 + * 2 属性 就理解成变量,跟普通变量相对多个 this + * + * + * + */ + diff --git "a/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/controller/UserController.js" "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/controller/UserController.js" new file mode 100644 index 0000000000000000000000000000000000000000..3a8b78a378c8f787fc16c2e11258eef9917e1b89 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/controller/UserController.js" @@ -0,0 +1,75 @@ +const BaseController = require("./BaseController"); +const UserModel = require("../../model/UserModel"); +const EncAndDec = require("../../untis/EecAndDec"); +let mysql=require("mysql") +class UserController extends BaseController { + async login() { + return this.display("login.html"); + } + async userReg(){ + return this.display("userReg.html"); + } + async handleReg(){ + let name = this.req.post['name']; + // let password =EncAndDec.md5(this.req.post['password']);导致 前后端密码不同 + let password = this.req.post['password']; + let sql="INSERT into user(name,password)VALUES('"+name+"' ,'"+password+"')"; + let userModel = new UserModel(); + let userList=await userModel.query(sql,[name,password]); + let userInfo={}; + if(userList.length>0){ + userInfo = userList[0]; + this.res.session = JSON.stringify(userInfo); + } + console.log(name); + console.log(password) + return this.display("login.html");; + } + async loginz(){ + + + // this.res.write(this.logins) + + return this.display("dlu.html"); + } + async logins(){ + //又没判断用户是否为空? 也没有判断,密码是否为空 + let name = this.req.post['name']; + let password =this.req.post['password']; //EncAndDec.md5(this.req.post['password']); + let sql = "select * from user where name=? and password=? limit 1"; + let userModel = new UserModel(); + let userList=await userModel.query(sql,[name,password]); + console.log(userList); + //写入到数据库 你们完善 TODO + //JSON.parse 把字符串还原成数组或者 json格式 + let userInfo={}; + if(userList.length>0){//长度大于0 说明查询到数据了,说明用户密码都是正确的 + userInfo = userList[0]; + this.res.session = JSON.stringify(userInfo); + // res.setHeader('Set-Cookie',['demo=123456','demo2=78910']); + + }else{ + + let int="密码或者账号错误" + + return int + } + + return JSON.stringify(userInfo)+this.display("dlu.html"); + + } + + + async userList(){ + let user = new UserModel(); + try{ + let userList = await user.select(); + return JSON.stringify(userList); + + }catch(err){ + console.log(err); + } + } + +} +module.exports = UserController; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/controller/index.js" "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/controller/index.js" new file mode 100644 index 0000000000000000000000000000000000000000..fb69ae86016b681a654ce9e296d5e5e183c54bbd --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/controller/index.js" @@ -0,0 +1,79 @@ +/** + * 在mvc 中,controller 是指挥者者,他指挥着数据获取与模板的渲染 + * + */ + let fs = require("fs"); +let index={ + + + index:async function(req,res){ + let mysql = require("../../model/base"); + let date = req.query['date']; + //console.log('date-----'+date); + let sql = 'select * from number where date=?'; + //console.log(res); + let result= await mysql.query(sql,date,res);//等待数据的返回,但是线程可以干别的事情,线程我们就理解为饭店的服务员, + //node只能请得起一个服务员, await + promise 表示,这一段代码需要同步(服务员招待了客户, + //客户告诉服务器,我自己来,你忙别的) 如果说这块是阻塞的,那请求2次,5*2 =10 + //如果不是阻塞的 + + console.log(result); + let html=''; + for (let val in result) { + html += '' + html += '' + result[val].city + "" + html += '' + result[val].nums + "" + html += '' + + } + + fs.readFile("./home/html/index.html", function (err, data) { + console.log(err); + let datas = data.toString(); + let formatHtml = datas.replace("{{result}}", html); + res.write(formatHtml); + res.end(); + + }) + + + }, + + show: async function(req,res){ + + let mysql = require("../../model/base"); + let date = req.query['date']; + //console.log('date-----'+date); + let sql = 'select * from number order by id desc limit 1 ';//取出最先的一条数据 + //console.log(res); + let result= await mysql.query(sql,date,res);//等待数据的返回,但是线程可以干别的事情,线程我们就理解为饭店的服务员, + //node只能请得起一个服务员, await + promise 表示,这一段代码需要同步(服务员招待了客户, + //客户告诉服务器,我自己来,你忙别的) 如果说这块是阻塞的,那请求2次,5*2 =10 + //如果不是阻塞的 + + console.log(result); + let html=''; + for (let val in result) { + html += '' + html += '' + result[val].city + "" + html += '' + result[val].nums + "" + html += '' + + } + + fs.readFile("./home/html/index.html", function (err, data) { + console.log(err); + let datas = data.toString(); + let formatHtml = datas.replace("{{result}}", html); + res.write(formatHtml); + res.end(); + + }) + + + } +} + +module.exports=index; + +// \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/.keep" "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/detail.html" "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/detail.html" new file mode 100644 index 0000000000000000000000000000000000000000..f6fbfa950367b48ccdd8dcc5e64b93c866cd2c53 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/detail.html" @@ -0,0 +1,9 @@ +{% include='header.html' %} +

各城市疫情

+ {% for item in data %} + + {{ item.city }} + {{ item.nums }} + + {% endfor %} +{% include='footer.html' %} \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/dlu.html" "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/dlu.html" new file mode 100644 index 0000000000000000000000000000000000000000..72d0259cce84ed43935a26ec4ca48b8c33f28841 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/dlu.html" @@ -0,0 +1,12 @@ + + + + + + Document + + + 获取14号数据 + 获取15号数据 + + \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/footer.html" "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/footer.html" new file mode 100644 index 0000000000000000000000000000000000000000..d619210ac5cfa2dd8f909c0a035f7a6128f9c6b0 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/footer.html" @@ -0,0 +1,5 @@ + + + \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/header.html" "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/header.html" new file mode 100644 index 0000000000000000000000000000000000000000..349ba5b9820c1d4ece42fd6fcdd457553b90c183 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/header.html" @@ -0,0 +1,10 @@ + + + + + + + + {{title}} + + diff --git "a/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/index.html" "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/index.html" new file mode 100644 index 0000000000000000000000000000000000000000..d5451621b464070fcf157d390e059b992e1f89a0 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/index.html" @@ -0,0 +1,10 @@ +{% include 'header.html' %} + +

各城市疫情

+{% for item in data %} + + {{ item.city }} + {{ item.nums }} + +{% endfor %} +{% include 'footer.html' %} \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/login.html" "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/login.html" new file mode 100644 index 0000000000000000000000000000000000000000..7397edd8ac6a2685e1601e783fdbcdbabc6a6a16 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/login.html" @@ -0,0 +1,35 @@ + + + + + + + + Document + + + +
+
+
+
+ +
+
+ + \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/userReg.html" "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/userReg.html" new file mode 100644 index 0000000000000000000000000000000000000000..ccd016e524733d38c6d288f5d4368cbc200c0a6d --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/home/html/userReg.html" @@ -0,0 +1,21 @@ + + + + + + + 注册于登陆 + + +
+
+ 用户名:
+ 密 码:
+ 确认密码:
+ +
+ +
+ + + \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220323cookie_session/model/.keep" "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/model/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220323cookie_session/model/Base.js" "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/model/Base.js" new file mode 100644 index 0000000000000000000000000000000000000000..937befcfe89b8956fe17e0c8286574e16469edca --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/model/Base.js" @@ -0,0 +1,60 @@ + + +let mysqlModule = require("mysql"); + +let db = require("../config/db"); + +class mysql { + + /** + * 初始化 + */ + constructor() { + + } + + + query(sql, value) { + + let promise = new Promise(function (resolve, reject) { + let connection = mysqlModule.createConnection({ host: db.host, user: db.user, password: db.password, database: db.database }); + + connection.connect(); + /** + * 推荐使用占位符方式查询,占位符可以防止 sql注入 占了 80%漏洞都是sql + */ + connection.query(sql, value, function (err, result) { + + if (err) { + reject(err.message) + } else { + + //setTimeout(function () {//模拟数据的慢查询 + resolve(result); + //},5000) + + } + + connection.end(); + }) + + + }) + return promise; + + } + + /** + * 简单的数据查询 + * @param {}} condtion + */ + select() { + + let sql = "select * from " + this.table; + return this.query(sql, '') + } + + +} + +module.exports = mysql; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220323cookie_session/model/UserModel.js" "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/model/UserModel.js" new file mode 100644 index 0000000000000000000000000000000000000000..f5b5c67efca3f38c5003c5d62a44877509c04256 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/model/UserModel.js" @@ -0,0 +1,20 @@ + +let Base = require("./Base"); + +class UserModel extends Base{ + + constructor(){ + super();//父亲构造器,意思说 父亲的构造器也要执行 + this.table = 'user'; + + } + + + + + + + +} + +module.exports = UserModel; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220323cookie_session/untis/EecAndDec.js" "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/untis/EecAndDec.js" new file mode 100644 index 0000000000000000000000000000000000000000..e5fa3450dbbb5c71ba545fbde06d99a9d31e8140 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/untis/EecAndDec.js" @@ -0,0 +1,15 @@ +let crypto = require("crypto"); + +let EncAndDec = { + + md5: function (data) { + let md5 = crypto.createHash("md5"); + + md5.update(data); + + return md5.digest("hex"); + + } +} + +module.exports=EncAndDec; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220323cookie_session/url\345\212\250\346\200\201\350\265\204\346\272\220.js" "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/url\345\212\250\346\200\201\350\265\204\346\272\220.js" new file mode 100644 index 0000000000000000000000000000000000000000..ff9851e36db722f8edfd42a2474ea55c0512a654 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220323cookie_session/url\345\212\250\346\200\201\350\265\204\346\272\220.js" @@ -0,0 +1,80 @@ +// 登入页优化,基于cookie 与session 把用户信息保存起来,下次请求时调取出用户信息 +let http = require("http"); +let fs = require("fs"); +let nunjucks = require("nunjucks"); +let server = http.createServer(); +server.listen(8080); +console.log("注册页面 输入中文会乱码 MD5注入数据库 前端不会翻译 "); +console.log("注册页面 输入英文或者数字 注册成功进入登陆页面"+ "http://localhost:8080/?a=userReg&c=user"); +console.log("进人登录页面 登录成功获取数据"+" http://localhost:8080/?a=login&c=user"); + +server.on('request', function (req, res) { + let postData = ''; + req.on('data', function (chunk) { + postData += chunk; + }); + req.on('end', async function () { + // console.log("流方式获取数据:::::"); + console.log(postData); + let postArr = postData.split("&");// ['name=demo','password=23']; + let postVals = []; + for (let ky in postArr) { + let val = postArr[ky];//'a=index'....... + let temPostArray = val.split("="); + postVals[temPostArray[0]] = temPostArray[1]; + } + req.post = postVals; + let url = req.url; + if (url.lastIndexOf('.') > -1) { + console.log(url); + let path = '.' + url; + readFileByPromise(path, res); + } + else { + res.setHeader("Content-type", "text/html;charset=utf8"); + let queryString = url.split("?")[1];//a=index&c=index + let queryArr = queryString.split("&");// ['a=index','c=index']; + let keyVals = []; + for (let key in queryArr) { + let vals = queryArr[key];//'a=index'....... + let temArray = vals.split("="); + keyVals[temArray[0]] = temArray[1]; + } + req.query = keyVals; + console.log(keyVals['c']); + let controllerPath = './home/controller/' + keyVals['c'].replace(keyVals[0], keyVals['c'][0].toUpperCase()) + "Controller"; + let controller = require(controllerPath); + console.log(controller); + let action = keyVals['a']; + let obj = new controller(req, res, nunjucks); + let content = await obj[action](); + res.setHeader('Set-Cookie',['demo=123456','demo2=78910']); + res.write(content); + res.end(); + } + }) +}) + +let promise = function (filename) { + + return new Promise(function (resolve, reject) { + + fs.exists(filename, function (result) { + if (!result) { + filename = './home/imgs/404.png'; + } + fs.readFile(filename, function (err, data) { + resolve(data); + }) + }) + }) +} + +async function readFileByPromise(fileName, res) { + let data = await promise(fileName); + if (data) { + res.write(data); + } + res.end(); +} + diff --git "a/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/config/db.js" "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/config/db.js" new file mode 100644 index 0000000000000000000000000000000000000000..e1b143475ee09e95e6de0637d7af3fac6d05c55e --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/config/db.js" @@ -0,0 +1,18 @@ +let dbconfig = { + + host: '127.0.0.1', + user: 'root', + password: '123456', + database: 'yiqing', + port: '3306' + +} +module.exports = dbconfig; + +//dbconfig.key(); + +// json 非常重要!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +// 其实它很单纯,看到 { 开头 }结尾就是json ,它是 key val 形式 ,key 就是 类型数组的键名,value 是对应的值 + +// value 不限定类型 \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/.keep" "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/controller/.keep" "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/controller/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/controller/BaseController.js" "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/controller/BaseController.js" new file mode 100644 index 0000000000000000000000000000000000000000..66d265e4dbefd72703b5274ae8b6b275183c87ad --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/controller/BaseController.js" @@ -0,0 +1,20 @@ + +class BaseController { + + constructor(req, res, nunjucks) { + + this.req = req; + this.res = res; + this.nunjucks = nunjucks; + this.nunjucks.configure('./home/html', { autoescape: true });//配置模板的目录 + + } + + display(path,data){ + return this.nunjucks.render(path,data); + } + + +} + +module.exports = BaseController; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/controller/IndexController.js" "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/controller/IndexController.js" new file mode 100644 index 0000000000000000000000000000000000000000..57d9af51225f4934757ba33f17176b678f6958aa --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/controller/IndexController.js" @@ -0,0 +1,119 @@ +const BaseController = require("./BaseController"); +let mysql = require("../../model/Base"); + +class IndexController extends BaseController { + + + async index() { + + let mysqlObj = new mysql(); + let date = this.req.query['date']; + //console.log('date-----'+date); + let sql = 'select * from number where date=?'; + //console.log(res); + try { + let result = await mysqlObj.query(sql, date);//等待数据的返回,但是线程可以干别的事情,线程我们就理解为饭店的服务员, + //node只能请得起一个服务员, await + promise 表示,这一段代码需要同步(服务员招待了客户, + //客户告诉服务器,我自己来,你忙别的) 如果说这块是阻塞的,那请求2次,5*2 =10 + //如果不是阻塞的 + //result 是啥类型?数组 + return this.display('index.html', { data: result,title:'我是index11111111' }) + } catch (err) { + console.log(err); + } + + } + + async detail() { + + let mysqlObj = new mysql(); + let date = this.req.query['date']; + //console.log('date-----'+date); + let sql = 'select * from number where date=?'; + //console.log(res); + try { + let result = await mysqlObj.query(sql, date);//等待数据的返回,但是线程可以干别的事情,线程我们就理解为饭店的服务员, + //node只能请得起一个服务员, await + promise 表示,这一段代码需要同步(服务员招待了客户, + //客户告诉服务器,我自己来,你忙别的) 如果说这块是阻塞的,那请求2次,5*2 =10 + //如果不是阻塞的 + //result 是啥类型?数组 + return this.display('detail.html', { data: result }) + } catch (err) { + console.log(err); + } + + } + + + diji() { + + console.log("盖地基"); + + } + + zhuzi() { + + console.log("盖柱子"); + + } + + qizhuang() { + + console.log("砌砖"); + + } + + quchunishuijiang() { + console.log(this.nishuijiang); + + } + + + +} +module.exports = IndexController; + +// let obj = new IndexController('李四');//开始去搭积木,比方说我想搭建个小房子 ,new 就表示 具体要做个工程了 +// // 类是不限制构建次数 +// obj.diji(); +// obj.zhuzi(); +// obj.qizhuang(); +// obj.quchunishuijiang(); + +// let obj2 = new IndexController('张三');//我们每次项目对象正常我们目的是不一样的 +// //this就表示每次构建的这个对象 +// obj2.quchunishuijiang(); + +// console.log(obj==obj2) + + +/** + * 回顾 class java es6 + * + * 面向对象编程三大特点:继承,封装,多态(方法的重写) + * + * 封装: + * 继承:extends 需要继承 + * + * + * + * + * class 中文翻译过来 班级 在编程里面叫 类,我们就按班级理解 + * + * 班级里有很多成员,单元可以有一个,元素 在js 里面就是 函数(js class 里面不写function 就是函数名与 {} , + * + * 在class 里面就不叫函数了,就叫 方法),和属性() + * + * class 的使用 , new 加一个class 名称,我们可以理解为, class 就是一堆的积木,里面有 圆柱体,有长方体.....(就是方法),但是我们积木 + * + * 没有构建成具体的建筑. + * + * + * class :元素 + * 1 函数,不用function 去标识,而且在类里面不叫函数了,叫方法 + * 2 属性 就理解成变量,跟普通变量相对多个 this + * + * + * + */ + diff --git "a/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/controller/UserController.js" "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/controller/UserController.js" new file mode 100644 index 0000000000000000000000000000000000000000..1622e6f73464a564c17724d8ec6f4ccf645e3fee --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/controller/UserController.js" @@ -0,0 +1,78 @@ +const BaseController = require("./BaseController"); +const UserModel = require("../../model/UserModel"); +const EncAndDec = require("../../untis/EecAndDec"); +let mysql=require("mysql") +class UserController extends BaseController { + async login() { + return this.display("login.html"); + } + async userReg(){ + return this.display("userReg.html"); + } + async handleReg(){ + let name = this.req.post['name']; + // let password =EncAndDec.md5(this.req.post['password']);导致 前后端密码不同 + let password = this.req.post['password']; + let sql="INSERT into user(name,password)VALUES('"+name+"' ,'"+password+"')"; + let userModel = new UserModel(); + let userList=await userModel.query(sql,[name,password]); + let userInfo={}; + if(userList.length>0){ + userInfo = userList[0]; + this.res.session = JSON.stringify(userInfo); + } + console.log(name); + console.log(password) + return this.display("login.html");; + } + async loginz(){ + + return this.display("dlu.html"); + } + + async gzt(){ + + return this.display("gzt.html"); + } + + + + async logins(){ + //又没判断用户是否为空? 也没有判断,密码是否为空 + let name = this.req.post['name']; + let password =this.req.post['password']; //EncAndDec.md5(this.req.post['password']); + let sql = "select * from user where name=? and password=? limit 1"; + let userModel = new UserModel(); + let userList=await userModel.query(sql,[name,password]); + console.log(userList); + //写入到数据库 你们完善 TODO + //JSON.parse 把字符串还原成数组或者 json格式 + let userInfo={}; + if(userList.length>0){//长度大于0 说明查询到数据了,说明用户密码都是正确的 + userInfo = userList[0]; + this.res.session = JSON.stringify(userInfo); + // res.setHeader('Set-Cookie',['demo=123456','demo2=78910']); + }else{ + + let int="密码或者账号错误" + + return int + } + return JSON.stringify(userInfo)+this.display("gzt.html"); + + } + + + async userList(){ + let user = new UserModel(); + try{ + let userList = await user.select(); + return JSON.stringify(userList); + + }catch(err){ + console.log(err); + } + } + +} +module.exports = UserController; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/controller/index.js" "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/controller/index.js" new file mode 100644 index 0000000000000000000000000000000000000000..fb69ae86016b681a654ce9e296d5e5e183c54bbd --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/controller/index.js" @@ -0,0 +1,79 @@ +/** + * 在mvc 中,controller 是指挥者者,他指挥着数据获取与模板的渲染 + * + */ + let fs = require("fs"); +let index={ + + + index:async function(req,res){ + let mysql = require("../../model/base"); + let date = req.query['date']; + //console.log('date-----'+date); + let sql = 'select * from number where date=?'; + //console.log(res); + let result= await mysql.query(sql,date,res);//等待数据的返回,但是线程可以干别的事情,线程我们就理解为饭店的服务员, + //node只能请得起一个服务员, await + promise 表示,这一段代码需要同步(服务员招待了客户, + //客户告诉服务器,我自己来,你忙别的) 如果说这块是阻塞的,那请求2次,5*2 =10 + //如果不是阻塞的 + + console.log(result); + let html=''; + for (let val in result) { + html += '' + html += '' + result[val].city + "" + html += '' + result[val].nums + "" + html += '' + + } + + fs.readFile("./home/html/index.html", function (err, data) { + console.log(err); + let datas = data.toString(); + let formatHtml = datas.replace("{{result}}", html); + res.write(formatHtml); + res.end(); + + }) + + + }, + + show: async function(req,res){ + + let mysql = require("../../model/base"); + let date = req.query['date']; + //console.log('date-----'+date); + let sql = 'select * from number order by id desc limit 1 ';//取出最先的一条数据 + //console.log(res); + let result= await mysql.query(sql,date,res);//等待数据的返回,但是线程可以干别的事情,线程我们就理解为饭店的服务员, + //node只能请得起一个服务员, await + promise 表示,这一段代码需要同步(服务员招待了客户, + //客户告诉服务器,我自己来,你忙别的) 如果说这块是阻塞的,那请求2次,5*2 =10 + //如果不是阻塞的 + + console.log(result); + let html=''; + for (let val in result) { + html += '' + html += '' + result[val].city + "" + html += '' + result[val].nums + "" + html += '' + + } + + fs.readFile("./home/html/index.html", function (err, data) { + console.log(err); + let datas = data.toString(); + let formatHtml = datas.replace("{{result}}", html); + res.write(formatHtml); + res.end(); + + }) + + + } +} + +module.exports=index; + +// \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/.keep" "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/detail.html" "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/detail.html" new file mode 100644 index 0000000000000000000000000000000000000000..f6fbfa950367b48ccdd8dcc5e64b93c866cd2c53 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/detail.html" @@ -0,0 +1,9 @@ +{% include='header.html' %} +

各城市疫情

+ {% for item in data %} + + {{ item.city }} + {{ item.nums }} + + {% endfor %} +{% include='footer.html' %} \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/dlu.html" "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/dlu.html" new file mode 100644 index 0000000000000000000000000000000000000000..401b3c7382ec2244012cb4c9876a0423c1e102aa --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/dlu.html" @@ -0,0 +1,16 @@ + + + + + + Document + + +
+ 获取14号数据 + 获取15号数据 + +
+ + + \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/footer.html" "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/footer.html" new file mode 100644 index 0000000000000000000000000000000000000000..d619210ac5cfa2dd8f909c0a035f7a6128f9c6b0 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/footer.html" @@ -0,0 +1,5 @@ + + + \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/gzt.html" "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/gzt.html" new file mode 100644 index 0000000000000000000000000000000000000000..f49ea0b2c582dce87df4a468d91be2b75ea70ddb --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/gzt.html" @@ -0,0 +1,21 @@ + + + + + + Document + + + +
+ +
+ + \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/header.html" "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/header.html" new file mode 100644 index 0000000000000000000000000000000000000000..349ba5b9820c1d4ece42fd6fcdd457553b90c183 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/header.html" @@ -0,0 +1,10 @@ + + + + + + + + {{title}} + + diff --git "a/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/index.html" "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/index.html" new file mode 100644 index 0000000000000000000000000000000000000000..d5451621b464070fcf157d390e059b992e1f89a0 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/index.html" @@ -0,0 +1,10 @@ +{% include 'header.html' %} + +

各城市疫情

+{% for item in data %} + + {{ item.city }} + {{ item.nums }} + +{% endfor %} +{% include 'footer.html' %} \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/login.html" "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/login.html" new file mode 100644 index 0000000000000000000000000000000000000000..7397edd8ac6a2685e1601e783fdbcdbabc6a6a16 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/login.html" @@ -0,0 +1,35 @@ + + + + + + + + Document + + + +
+
+
+
+ +
+
+ + \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/userReg.html" "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/userReg.html" new file mode 100644 index 0000000000000000000000000000000000000000..ccd016e524733d38c6d288f5d4368cbc200c0a6d --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/home/html/userReg.html" @@ -0,0 +1,21 @@ + + + + + + + 注册于登陆 + + +
+
+ 用户名:
+ 密 码:
+ 确认密码:
+ +
+ +
+ + + \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/model/.keep" "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/model/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/model/Base.js" "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/model/Base.js" new file mode 100644 index 0000000000000000000000000000000000000000..937befcfe89b8956fe17e0c8286574e16469edca --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/model/Base.js" @@ -0,0 +1,60 @@ + + +let mysqlModule = require("mysql"); + +let db = require("../config/db"); + +class mysql { + + /** + * 初始化 + */ + constructor() { + + } + + + query(sql, value) { + + let promise = new Promise(function (resolve, reject) { + let connection = mysqlModule.createConnection({ host: db.host, user: db.user, password: db.password, database: db.database }); + + connection.connect(); + /** + * 推荐使用占位符方式查询,占位符可以防止 sql注入 占了 80%漏洞都是sql + */ + connection.query(sql, value, function (err, result) { + + if (err) { + reject(err.message) + } else { + + //setTimeout(function () {//模拟数据的慢查询 + resolve(result); + //},5000) + + } + + connection.end(); + }) + + + }) + return promise; + + } + + /** + * 简单的数据查询 + * @param {}} condtion + */ + select() { + + let sql = "select * from " + this.table; + return this.query(sql, '') + } + + +} + +module.exports = mysql; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/model/UserModel.js" "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/model/UserModel.js" new file mode 100644 index 0000000000000000000000000000000000000000..f5b5c67efca3f38c5003c5d62a44877509c04256 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/model/UserModel.js" @@ -0,0 +1,20 @@ + +let Base = require("./Base"); + +class UserModel extends Base{ + + constructor(){ + super();//父亲构造器,意思说 父亲的构造器也要执行 + this.table = 'user'; + + } + + + + + + + +} + +module.exports = UserModel; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/untis/EecAndDec.js" "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/untis/EecAndDec.js" new file mode 100644 index 0000000000000000000000000000000000000000..e5fa3450dbbb5c71ba545fbde06d99a9d31e8140 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/untis/EecAndDec.js" @@ -0,0 +1,15 @@ +let crypto = require("crypto"); + +let EncAndDec = { + + md5: function (data) { + let md5 = crypto.createHash("md5"); + + md5.update(data); + + return md5.digest("hex"); + + } +} + +module.exports=EncAndDec; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/url\345\212\250\346\200\201\350\265\204\346\272\220.js" "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/url\345\212\250\346\200\201\350\265\204\346\272\220.js" new file mode 100644 index 0000000000000000000000000000000000000000..a1154752d3f783f4c88fd91deae3d76f8656b174 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220324cookie_session\347\232\204\345\272\224\347\224\250/url\345\212\250\346\200\201\350\265\204\346\272\220.js" @@ -0,0 +1,80 @@ +// 登入页优化,基于cookie 与session # 登录页面样式完善下,设计一个登入后的工作页面 +let http = require("http"); +let fs = require("fs"); +let nunjucks = require("nunjucks"); +let server = http.createServer(); +server.listen(8080); +console.log("注册页面 输入中文会乱码 MD5注入数据库 前端不会翻译 "); +console.log("注册页面 输入英文或者数字 注册成功进入登陆页面"+ "http://localhost:8080/?a=userReg&c=user"); +console.log("进入登录页面 登录成功进入工作台"+" http://localhost:8080/?a=login&c=user"); + +server.on('request', function (req, res) { + let postData = ''; + req.on('data', function (chunk) { + postData += chunk; + }); + req.on('end', async function () { + // console.log("流方式获取数据:::::"); + console.log(postData); + let postArr = postData.split("&");// ['name=demo','password=23']; + let postVals = []; + for (let ky in postArr) { + let val = postArr[ky];//'a=index'....... + let temPostArray = val.split("="); + postVals[temPostArray[0]] = temPostArray[1]; + } + req.post = postVals; + let url = req.url; + if (url.lastIndexOf('.') > -1) { + console.log(url); + let path = '.' + url; + readFileByPromise(path, res); + } + else { + res.setHeader("Content-type", "text/html;charset=utf8"); + let queryString = url.split("?")[1];//a=index&c=index + let queryArr = queryString.split("&");// ['a=index','c=index']; + let keyVals = []; + for (let key in queryArr) { + let vals = queryArr[key];//'a=index'....... + let temArray = vals.split("="); + keyVals[temArray[0]] = temArray[1]; + } + req.query = keyVals; + console.log(keyVals['c']); + let controllerPath = './home/controller/' + keyVals['c'].replace(keyVals[0], keyVals['c'][0].toUpperCase()) + "Controller"; + let controller = require(controllerPath); + console.log(controller); + let action = keyVals['a']; + let obj = new controller(req, res, nunjucks); + let content = await obj[action](); + res.setHeader('Set-Cookie',['demo=123456','demo2=78910']); + res.write(content); + res.end(); + } + }) +}) + +let promise = function (filename) { + + return new Promise(function (resolve, reject) { + + fs.exists(filename, function (result) { + if (!result) { + filename = './home/imgs/404.png'; + } + fs.readFile(filename, function (err, data) { + resolve(data); + }) + }) + }) +} + +async function readFileByPromise(fileName, res) { + let data = await promise(fileName); + if (data) { + res.write(data); + } + res.end(); +} + diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/.keep" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/ThinkNode.js" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/ThinkNode.js" new file mode 100644 index 0000000000000000000000000000000000000000..b400974476b525d8a75ed5b2d6f8cac50ceeea44 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/ThinkNode.js" @@ -0,0 +1,215 @@ +let fs = require("fs"); +let nunjucks = require("nunjucks"); +let ArrayHelp = require("./untis/ArrayHelp"); +let dbCofig = require('./config/db'); +let mysql = require("mysql"); +let staticFileType = ['.ico', '.gif', '.png', '.jpg', '.css', '.js']; +let ThinkNode = { + + buildController: function () { + let connection = mysql.createConnection({ host: dbCofig.host, user: dbCofig.user, password: dbCofig.password, database: dbCofig.database, port: dbCofig.port }) + connection.createConnection(); + //查询所以的表 + let tables = 'show tables' + connection.query(tables, function (err, result) { + if (err) { + console.log(err); + } else { + + + } + + }) + + + }, + staticFileHandle: function (path, res) { + fs.readFile(path, function (err, data) { + if (err) { + console.log(err); + } else { + res.end(data); + } + + }) + + }, + findControllAndHandle: async function (req, res, funcend,session) { + let url = req.url; + let isStatic = false; + //静态资源 + staticFileType.forEach(element => { + if (url.lastIndexOf(element) > -1) { + isStatic = true; + + let path = '.' +decodeURI(url); + ThinkNode.staticFileHandle(path, res); + } + + }); + if (isStatic == false) { + res.setHeader("Content-Type", "text/html; charset=utf-8"); + let controllerAndAction = ArrayHelp.buildKeyAndVal(url.split("?")[1], '&', "="); + let front = controllerAndAction["f"] ? controllerAndAction["f"] : 'home'; + let controller = controllerAndAction["c"] ? controllerAndAction["c"] : 'Index'; + let action = controllerAndAction["a"] ? controllerAndAction["a"] : 'index'; + let controllerPath = './' + front + '/controller/' + controller.replace(controller[0], controller[0].toUpperCase()) + "Controller.js"; + fs.stat(controllerPath, async function (err, stats) { + if (err) { + console.log('控制器找不到,请确认url中c 对应的值得控制器是否存在!'); + } else { + let classFile = require(controllerPath); + try { + let obj = new classFile(req, res, nunjucks); + if (obj.__proto__.hasOwnProperty(action)) {//判断该类中是否存在该方法 + let content = await obj[action](); + //console.log('业务逻辑执行的结果'+content); + //前置中间件 + //var i = 0; + //ThinkNode.next(funcsend, i, req, res, content); + res.write(content); + //写入session + if(res.session){ + console.log(res.session,req.session); + let newSession=extend(req.session,res.session); + delete(session[req.cookie.node_session]); + session[req.cookie.node_session]=newSession; + } + res.end(); + return content; + } else { + //console.log('no method'); + res.end('

八戒你又调皮了,这个类中不存在' + action + '方法

'); + } + + } catch (err) { + console.log(err); + //报错结束请求响应 + res.end(); + } + + } + }) + + } + + }, + + next: function (funcs, i, req, res, content) { + if (isNaN(i)) return; + var task = funcs[i++]; // 取出函数数组里的下一个函数 + if (!task) { // 如果函数不存在,return + return; + } + if (content) { + task(req, res, content, ThinkNode.next(funcs, i, req, res)); // 否则,执行下一个函数 + } else { + task(req, res, ThinkNode.next(funcs, i, req, res)); // 否则,执行下一个函数 + } + }, + + parseDataAndHanle: function (req, res, funcsend,session) { + let postData = ''; + req.on('data', function (chunk) {//流方式读取数据,数据一段一段传输,这一段代码是异步的 + postData += chunk; + }); + + req.on('end', async function () { + //解析提交数据 + let data = ArrayHelp.buildKeyAndVal(postData, '', "="); + //把get和post数据都写入到query + req.query = data; + //如果是get方式把数据写入到 get 属性中 + if (req.method == 'get') { + req.get = data; + } + //如果是post方式把数据写入到 post 属性中 + if (req.method == 'post') { + req.post = data; + } + //解析cookie + req.cookie = ArrayHelp.buildKeyAndVal(req.headers.cookie, '; ', '='); + res.session={}; + req.session={}; + node_session=null; + //如果没有 node_session 则设置一个 + if (!req.cookie.node_session) { + node_session = new Date().getTime() + '_' + Math.random(); + res.setHeader('Set-Cookie', "node_session=" + node_session); + req.cookie.node_session=node_session; + // fs.exists('./session'),function(result){ + // if(!result){ + // fs.mkdir('./session',function(err){ + // fs.writeFile('./session/'+node_session+'.txt',function(){ + // ThinkNode.findControllAndHandle(req, res, funcsend); + // }) + // }) + // }else{ + // ThinkNode.findControllAndHandle(req, res, funcsend); + // } + // } + + } else { + node_session=req.cookie.node_session; + if(session[node_session]){ + req.session=session[node_session]; + } + // //session以文件存储 + // let sessionId = req.cookie.node_session; + // //获取session的数据 + // fs.readFile("./session/" + sessionId + ".txt", function (err, data) { + // //执行控制器,与后置控制器 + // let session = data; + // req.session = session; + // ThinkNode.findControllAndHandle(req, res, funcsend); + // }) + } + ThinkNode.findControllAndHandle(req, res, funcsend,session); + }) + }, + server: function () { + + var funcs = []; // 待执行的函数数组----前置控制器 + var funcsend = []; // 待执行的函数数组----后置控制器 + let session=[]; + var app = function (req, res) { + var i = 0; + //前置中间件 + ThinkNode.next(funcs, i, req, res); + //中间件end + //执行解析数据与控制器与后置控制器 + ThinkNode.parseDataAndHanle(req, res, funcsend,session); + //console.log(session); + } + + /** + * use方法就是把函数添加到函数数组中---前置控制器 + * @param task + */ + app.use = function (task) { + funcs.push(task); + } + + /** + * use方法就是把函数添加到函数数组中---后置控制器 + * @param task + */ + app.useend = function (task) { + funcsend.push(task); + } + + return app; // 返回实例 + } + + +} +function extend(target, source) { + for (var obj in source) { + target[obj] = source[obj]; + } + return target; +} + + +module.exports = ThinkNode; + diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/app.js" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/app.js" new file mode 100644 index 0000000000000000000000000000000000000000..b215410026c62d1e574abe2cac338f5852c48edf --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/app.js" @@ -0,0 +1,36 @@ +/** +//////////////////////////////////////////////////////////////////// +// _ooOoo_ // +// o8888888o // +// 88" . "88 // +// (| ^_^ |) // +// O\ = /O // +// ____/`---'\____ // +// .' \\| |// `. // +// / \\||| : |||// \ // +// / _||||| -:- |||||- \ // +// | | \\\ - /// | | // +// | \_| ''\---/'' | | // +// \ .-\__ `-` ___/-. / // +// ___`. .' /--.--\ `. . ___ // +// ."" '< `.___\_<|>_/___.' >'"". // +// | | : `- \`.;`\ _ /`;.`/ - ` : | | // +// \ \ `-. \_ __\ /__ _/ .-` / / // +// ========`-.____`-.___\_____/___.-`____.-'======== // +// `=---=' // +// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // +// 佛祖保佑 永不宕机 永无BUG // +//////////////////////////////////////////////////////////////////// + * + * + * + * + * / + */ + +let http= require("http"); +let ThinkNode = require("./ThinkNode"); +let app=ThinkNode.server(); + +http.createServer(app).listen(8080); + diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/config/application.js" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/config/application.js" new file mode 100644 index 0000000000000000000000000000000000000000..98c1dee09358221cd0c4600805ac991412a45d64 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/config/application.js" @@ -0,0 +1,5 @@ +let application={ + even:'dev' +} + +module.exports=application; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/config/db.js" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/config/db.js" new file mode 100644 index 0000000000000000000000000000000000000000..e1b143475ee09e95e6de0637d7af3fac6d05c55e --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/config/db.js" @@ -0,0 +1,18 @@ +let dbconfig = { + + host: '127.0.0.1', + user: 'root', + password: '123456', + database: 'yiqing', + port: '3306' + +} +module.exports = dbconfig; + +//dbconfig.key(); + +// json 非常重要!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +// 其实它很单纯,看到 { 开头 }结尾就是json ,它是 key val 形式 ,key 就是 类型数组的键名,value 是对应的值 + +// value 不限定类型 \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/expressdemo.js" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/expressdemo.js" new file mode 100644 index 0000000000000000000000000000000000000000..9dd1648016fe1c00b14d6e2502260828483c5775 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/expressdemo.js" @@ -0,0 +1,37 @@ + + + +let http= require("http"); + +function app(){ + var urls = []; // + var app = function (req, res) { + let url = req.url; + for(let key in urls){ + if(url==key){ + urls[url](req,res); + } + } + res.end(); + } + + /** + * use方法就是把函数添加到函数数组中---前置控制器 + * @param task + */ + app.get = function (url,task) { + urls[url]=task; + } + return app; // 返回实例 +} + +let a= app(); +a.get('/demo.html',function(req,res){ + res.write("this is demo"); + }) +a.get('/demo2.html',function(req,res){ + res.write("this is demo2"); + }) +http.createServer(a).listen(8080); + + diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/favicon.ico" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/favicon.ico" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/.keep" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/controller/.keep" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/controller/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/controller/BaseController.js" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/controller/BaseController.js" new file mode 100644 index 0000000000000000000000000000000000000000..8d808f819a52672f1c650c26549e4882f6330cff --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/controller/BaseController.js" @@ -0,0 +1,42 @@ + + + +class BaseController { + + constructor(req, res, nunjucks) { + + this.req = req; + this.res = res; + this.nunjucks = nunjucks; + this.nunjucks.configure('./home/html', { autoescape: true });//配置模板的目录 + + } + + setSession(key, val) { + this.res.session[key] = val; + } + + getSession(key) { + //console.log(this.req.session); + return this.req.session[key]; + } + + setCookie(key, val, expries) { + this.res.setHeader('Set-Cookie', key + "=" + val + "; expries:" + expries); + } + + getCookie(key) { + //console.log('cookie json'); + //console.log(this.req.cookie); + return this.req.cookie[key]; + } + + + display(path, data) { + return this.nunjucks.render(path, data); + } + + +} + +module.exports = BaseController; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/controller/IndexController.js" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/controller/IndexController.js" new file mode 100644 index 0000000000000000000000000000000000000000..04d2d41764be758a870e9409c4a5797d8127f771 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/controller/IndexController.js" @@ -0,0 +1,124 @@ +const BaseController = require("./BaseController"); +let mysql = require("../../model/Base"); + +class IndexController extends BaseController { + + + async index() { + + return "

你好世界!

"; + } + + async list(){ + + let mysqlObj = new mysql(); + let date = this.req.query['date']?this.req.query['date']:'2022-3-15'; + //console.log('date-----'+date); + let sql = 'select * from number where date=?'; + //console.log(res); + try { + let result = await mysqlObj.query(sql, date);//等待数据的返回,但是线程可以干别的事情,线程我们就理解为饭店的服务员, + //node只能请得起一个服务员, await + promise 表示,这一段代码需要同步(服务员招待了客户, + //客户告诉服务器,我自己来,你忙别的) 如果说这块是阻塞的,那请求2次,5*2 =10 + //如果不是阻塞的 + //result 是啥类型?数组 + return this.display('index.html', { data: result,title:'我是index11111111' }) + } catch (err) { + console.log(err); + } + + } + + async detail() { + + let mysqlObj = new mysql(); + let date = this.req.query['date']; + //console.log('date-----'+date); + let sql = 'select * from number where date=?'; + //console.log(res); + try { + let result = await mysqlObj.query(sql, date);//等待数据的返回,但是线程可以干别的事情,线程我们就理解为饭店的服务员, + //node只能请得起一个服务员, await + promise 表示,这一段代码需要同步(服务员招待了客户, + //客户告诉服务器,我自己来,你忙别的) 如果说这块是阻塞的,那请求2次,5*2 =10 + //如果不是阻塞的 + //result 是啥类型?数组 + return this.display('detail.html', { data: result }) + } catch (err) { + console.log(err); + } + + } + + + diji() { + + console.log("盖地基"); + + } + + zhuzi() { + + console.log("盖柱子"); + + } + + qizhuang() { + + console.log("砌砖"); + + } + + quchunishuijiang() { + console.log(this.nishuijiang); + + } + + + +} +module.exports = IndexController; + +// let obj = new IndexController('李四');//开始去搭积木,比方说我想搭建个小房子 ,new 就表示 具体要做个工程了 +// // 类是不限制构建次数 +// obj.diji(); +// obj.zhuzi(); +// obj.qizhuang(); +// obj.quchunishuijiang(); + +// let obj2 = new IndexController('张三');//我们每次项目对象正常我们目的是不一样的 +// //this就表示每次构建的这个对象 +// obj2.quchunishuijiang(); + +// console.log(obj==obj2) + + +/** + * 回顾 class java es6 + * + * 面向对象编程三大特点:继承,封装,多态(方法的重写) + * + * 封装: + * 继承:extends 需要继承 + * + * + * + * + * class 中文翻译过来 班级 在编程里面叫 类,我们就按班级理解 + * + * 班级里有很多成员,单元可以有一个,元素 在js 里面就是 函数(js class 里面不写function 就是函数名与 {} , + * + * 在class 里面就不叫函数了,就叫 方法),和属性() + * + * class 的使用 , new 加一个class 名称,我们可以理解为, class 就是一堆的积木,里面有 圆柱体,有长方体.....(就是方法),但是我们积木 + * + * 没有构建成具体的建筑. + * + * + * class :元素 + * 1 函数,不用function 去标识,而且在类里面不叫函数了,叫方法 + * 2 属性 就理解成变量,跟普通变量相对多个 this + * + * + * + */ + diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/controller/UserController.js" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/controller/UserController.js" new file mode 100644 index 0000000000000000000000000000000000000000..4029939b9479d7fe019ba887c928a2e1cf35e4b2 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/controller/UserController.js" @@ -0,0 +1,69 @@ +const BaseController = require("./BaseController"); + +const UserModel = require("../../model/UserModel"); +const EncAndDec = require("../../untis/EecAndDec"); + + +class UserController extends BaseController { + + async login() { + this.setCookie("userLogin","ok"); + this.setSession('userInfo',{name:123}); + return this.display("login.html"); + } + + async insert() { + let user = new UserModel(); + return await user.insert({name:'admin2',password:'654321'}); + } + + async delete() { + let user = new UserModel(); + return await user.delete({name:'admin2'}); + } + + + async update() { + let user = new UserModel(); + return await user.update({name:'admin2'},{id:3}); + } + + /** + * 前台用户注册页面 + * @returns + */ + async userReg(){ + return this.display("userReg.html"); + } + + /** + * + * @returns 用户注册后端逻辑 + */ + async handleReg(){ + + //let name = this.req.post['name']; + //let password = EncAndDec.md5(this.req.post['password']); + //写入到数据库 你们完善 TODO + return this.display("userReg.html"); + } + + async userList(){ + let cookie = this.getCookie('userLogin'); + console.log('session 取到的值'+cookie); + console.log(this.getSession('userInfo')); + let user = new UserModel(); + + try{ + let userList = await user.find({name:'demo',password:'123456'}); + return JSON.stringify(userList);//就是把对象转成json格式 + + }catch(err){ + console.log(err); + } + + + } + +} +module.exports = UserController; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/controller/index.js" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/controller/index.js" new file mode 100644 index 0000000000000000000000000000000000000000..fb69ae86016b681a654ce9e296d5e5e183c54bbd --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/controller/index.js" @@ -0,0 +1,79 @@ +/** + * 在mvc 中,controller 是指挥者者,他指挥着数据获取与模板的渲染 + * + */ + let fs = require("fs"); +let index={ + + + index:async function(req,res){ + let mysql = require("../../model/base"); + let date = req.query['date']; + //console.log('date-----'+date); + let sql = 'select * from number where date=?'; + //console.log(res); + let result= await mysql.query(sql,date,res);//等待数据的返回,但是线程可以干别的事情,线程我们就理解为饭店的服务员, + //node只能请得起一个服务员, await + promise 表示,这一段代码需要同步(服务员招待了客户, + //客户告诉服务器,我自己来,你忙别的) 如果说这块是阻塞的,那请求2次,5*2 =10 + //如果不是阻塞的 + + console.log(result); + let html=''; + for (let val in result) { + html += '' + html += '' + result[val].city + "" + html += '' + result[val].nums + "" + html += '' + + } + + fs.readFile("./home/html/index.html", function (err, data) { + console.log(err); + let datas = data.toString(); + let formatHtml = datas.replace("{{result}}", html); + res.write(formatHtml); + res.end(); + + }) + + + }, + + show: async function(req,res){ + + let mysql = require("../../model/base"); + let date = req.query['date']; + //console.log('date-----'+date); + let sql = 'select * from number order by id desc limit 1 ';//取出最先的一条数据 + //console.log(res); + let result= await mysql.query(sql,date,res);//等待数据的返回,但是线程可以干别的事情,线程我们就理解为饭店的服务员, + //node只能请得起一个服务员, await + promise 表示,这一段代码需要同步(服务员招待了客户, + //客户告诉服务器,我自己来,你忙别的) 如果说这块是阻塞的,那请求2次,5*2 =10 + //如果不是阻塞的 + + console.log(result); + let html=''; + for (let val in result) { + html += '' + html += '' + result[val].city + "" + html += '' + result[val].nums + "" + html += '' + + } + + fs.readFile("./home/html/index.html", function (err, data) { + console.log(err); + let datas = data.toString(); + let formatHtml = datas.replace("{{result}}", html); + res.write(formatHtml); + res.end(); + + }) + + + } +} + +module.exports=index; + +// \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/html/.keep" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/html/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/html/detail.html" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/html/detail.html" new file mode 100644 index 0000000000000000000000000000000000000000..f6fbfa950367b48ccdd8dcc5e64b93c866cd2c53 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/html/detail.html" @@ -0,0 +1,9 @@ +{% include='header.html' %} +

各城市疫情

+ {% for item in data %} + + {{ item.city }} + {{ item.nums }} + + {% endfor %} +{% include='footer.html' %} \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/html/footer.html" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/html/footer.html" new file mode 100644 index 0000000000000000000000000000000000000000..d619210ac5cfa2dd8f909c0a035f7a6128f9c6b0 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/html/footer.html" @@ -0,0 +1,5 @@ + + + \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/html/header.html" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/html/header.html" new file mode 100644 index 0000000000000000000000000000000000000000..349ba5b9820c1d4ece42fd6fcdd457553b90c183 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/html/header.html" @@ -0,0 +1,10 @@ + + + + + + + + {{title}} + + diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/html/index.html" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/html/index.html" new file mode 100644 index 0000000000000000000000000000000000000000..d5451621b464070fcf157d390e059b992e1f89a0 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/html/index.html" @@ -0,0 +1,10 @@ +{% include 'header.html' %} + +

各城市疫情

+{% for item in data %} + + {{ item.city }} + {{ item.nums }} + +{% endfor %} +{% include 'footer.html' %} \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/html/login.html" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/html/login.html" new file mode 100644 index 0000000000000000000000000000000000000000..02a802e5faecc1398cb6082d884bb886579c81d8 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/html/login.html" @@ -0,0 +1,86 @@ + + + + + + + Document + + + +
+
+ +
+ 用户名:  
+
+
+ 密码   :   
+
+
+
+ +
+
+ + \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/html/userReg.html" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/html/userReg.html" new file mode 100644 index 0000000000000000000000000000000000000000..a29c3853c56a5316c0c562adcbd1680f0598c647 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/html/userReg.html" @@ -0,0 +1,89 @@ + + + + + + + + 注册于登陆 + + + +
+
+ +
+ 用户名:  
+
+
+ 密码   :   
+
+
+
+ +
+
+ + \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/imgs/\345\277\231\347\242\214.png" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/imgs/\345\277\231\347\242\214.png" new file mode 100644 index 0000000000000000000000000000000000000000..8b002453d3b03de023460d0e67ccc94468d58c4a Binary files /dev/null and "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/imgs/\345\277\231\347\242\214.png" differ diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/imgs/\346\210\220\345\212\237.png" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/imgs/\346\210\220\345\212\237.png" new file mode 100644 index 0000000000000000000000000000000000000000..835296236af44c56893e5ba5e24e555e7b3d8ec9 Binary files /dev/null and "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/imgs/\346\210\220\345\212\237.png" differ diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/imgs/\346\212\223\350\231\253\345\255\220.png" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/imgs/\346\212\223\350\231\253\345\255\220.png" new file mode 100644 index 0000000000000000000000000000000000000000..0d5b0df235877c2a02bdc68d327aeabbe1f93256 Binary files /dev/null and "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/imgs/\346\212\223\350\231\253\345\255\220.png" differ diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/imgs/\346\270\270\346\263\263\345\234\210.png" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/imgs/\346\270\270\346\263\263\345\234\210.png" new file mode 100644 index 0000000000000000000000000000000000000000..dca176d3025c9df32fc720de621b59ee8df5a227 Binary files /dev/null and "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/imgs/\346\270\270\346\263\263\345\234\210.png" differ diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/imgs/\347\234\213\344\271\246.png" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/imgs/\347\234\213\344\271\246.png" new file mode 100644 index 0000000000000000000000000000000000000000..813a644d631c4566262d6c37031e2b28940c8730 Binary files /dev/null and "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/imgs/\347\234\213\344\271\246.png" differ diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/imgs/\350\207\252\346\213\215.png" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/imgs/\350\207\252\346\213\215.png" new file mode 100644 index 0000000000000000000000000000000000000000..9800d9f8aa68dc7d6891bff1ea7a568faa1f3290 Binary files /dev/null and "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/home/imgs/\350\207\252\346\213\215.png" differ diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/model/.keep" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/model/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/model/Base.js" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/model/Base.js" new file mode 100644 index 0000000000000000000000000000000000000000..f2416e1ff6496d4ffa13557fd1fb6616ff863160 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/model/Base.js" @@ -0,0 +1,169 @@ + + +let mysqlModule = require("mysql"); + +let db = require("../config/db"); + +let application = require("../config/application"); + +class mysql { + + /** + * 初始化 + */ + constructor() { + + } + + + query(sql, value) { + + let promise = new Promise(function (resolve, reject) { + let connection = mysqlModule.createConnection({ host: db.host, user: db.user, password: db.password, database: db.database }); + + connection.connect(); + /** + * 推荐使用占位符方式查询,占位符可以防止 sql注入 占了 80%漏洞都是sql + */ + connection.query(sql, value, function (err, result) { + + if (err) { + console.log(sql); + console.log(value); + reject(err.message) + } else { + if (application.even == "dev") {//开发环境也打印sql + console.log('执行的sql=====' + sql); + console.log('sql对应的值'); + console.log(value); + } + //setTimeout(function () {//模拟数据的慢查询 + resolve(result); + //},5000) + + } + + connection.end(); + }) + + + }) + return promise; + + } + + /** + * 简单的数据查询 + * @param {}} condtion json 格式 + */ + async select(condition, limit) { + let sql = "select * from " + this.table; + let vals = []; + if (condition) { + sql += ' where '; + console.log(condition); + for (let element in condition) { + sql += ' ' + element + '=? and'; + vals.push(condition[element]); + }; + sql = sql.replace(new RegExp('\\' + 'and' + '+$', 'g'), ''); + } + if (!limit) { + limit = 20; + } + sql += ' limit ' + limit; + return await this.query(sql, vals) + } + + /** + * 查询一条数据 + * @param condtion + */ + async find(condition) { + let result = await this.select(condition, 1); + if (result.length == 0) { + return {}; + } + return result[0]; + } + + /** + * 删除数据 + * @param condtion + */ + async delete(condition) { + if (!condition) { + throw Error('写入数据为空!'); + }; + let sql="delete from "+this.table+' where '; + let vals=[]; + for (let element in condition) { + sql += ' ' + element + '=? and'; + vals.push(condition[element]); + }; + sql = sql.replace(new RegExp('\\' + 'and' + '+$', 'g'), ''); + return await this.query(sql, vals); + } + + + + /** + * condition 为json 格式 + * 写入数据 + * @param condtion + */ + async insert(condition) { + if (!condition) { + throw Error('写入数据为空!'); + }; + let sql = "insert into " + this.table + "("; + let vals = 'values ('; + let valArr = []; + for (let key in condition) { + sql += key + ","; + vals += '?,'; + valArr.push(condition[key]); + } + sql = sql.replace(new RegExp('\\' + ',' + '+$', 'g'), ''); + sql += ')' + vals = vals.replace(new RegExp('\\' + ',' + '+$', 'g'), ''); + vals += ')'; + sql += vals + let result = await this.query(sql, valArr); + return result; + } + + /** + * 删除数据 + * @param condtion + */ + async update(data,condition) { + if (!condition) { + throw Error('更新条件为空数据为空!'); + }; + if (!data) { + throw Error('更新数据不能为空!'); + }; + let sql="update "+this.table+' set '; + let valArr=[]; + for (let ele in data) { + //sql += ' ' + element + '=? and'; + sql+=' ' + ele + '=? ,'; + valArr.push(data[ele]); + }; + sql = sql.replace(new RegExp('\\' + ',' + '+$', 'g'), ''); + sql+=' where '; + for (let element in condition) { + //sql += ' ' + element + '=? and'; + sql+=' ' + element + '=? and'; + valArr.push(condition[element]); + }; + sql = sql.replace(new RegExp('\\' + 'and' + '+$', 'g'), ''); + return await this.query(sql, valArr); + } + + + +} + +module.exports = mysql; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/model/UserModel.js" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/model/UserModel.js" new file mode 100644 index 0000000000000000000000000000000000000000..f1d3b260542215c308012ec69cabfc495c45a5db --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/model/UserModel.js" @@ -0,0 +1,14 @@ + +let Base = require("./Base"); + +class UserModel extends Base{ + + constructor(){ + super();//父亲构造器,意思说 父亲的构造器也要执行 + this.table = 'user'; + + } + +} + +module.exports = UserModel; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/requestdemo.js" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/requestdemo.js" new file mode 100644 index 0000000000000000000000000000000000000000..f7875ad989335cacc7ccd4808bb3ecfc0dcdbdb5 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/requestdemo.js" @@ -0,0 +1,32 @@ +let request = require("request"); +//let url ='https://voice.baidu.com/act/newpneumonia/newpneumonia/?from=osari_aladin_banner'; +let fs = require("fs"); +//let url='https://api.gugudata.com/news/joke'; +//let url ="https://api.gugudata.com/sms/mobileattribution/demo"; +let yiqingapi="https://interface.sina.cn/news/wap/fymap2020_data.d.json"; + +// xpath 模块这个就能解析html 元素,但是页面实现方式会绕一点,可以使用正则(正则表达式)去匹配, + +// + +//演示了get方式请求数据,模拟提交表单post提交 +request.get(yiqingapi,{},function(err,result,body){ + if(err){ + console.log(err); + }else{ + //console.log(body); + //字符串怎么转成json格式? JSON.parse + let obj = JSON.parse(body); + //console.log(obj.list) + let list = obj.data.list; + list.forEach(ele => { + console.log(ele.name); + console.log(ele.ename); + console.log(ele.adddaily.city.name) + }); + + } + //console.log(arguments); + fs.writeFileSync('./yiqing1.html',body); + //http 协议,又没头信息?http协议请求都会发送头信息 +}); \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/untis/ArrayHelp.js" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/untis/ArrayHelp.js" new file mode 100644 index 0000000000000000000000000000000000000000..87fb7dccc4f6f3ba6ce507ef8004b26c83e343ee --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/untis/ArrayHelp.js" @@ -0,0 +1,24 @@ +let ArrayHelp={ + + /** + * 构建key val形式数组 + * @param {} String + * @param {*} firstSym + * @param {*} sendSym + * @returns + */ + buildKeyAndVal:function(String,firstSym,sendSym){ + if(!String) return []; + let strArr = String.split(firstSym);// ['name=demo','password=23']; + let vals = {}; + for (let ky in strArr) { + let val = strArr[ky];//'a=index'....... + let temArray = val.split(sendSym); + vals[temArray[0]] = temArray[1]; + } + return vals; + } + + +} +module.exports=ArrayHelp; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/untis/EecAndDec.js" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/untis/EecAndDec.js" new file mode 100644 index 0000000000000000000000000000000000000000..e5fa3450dbbb5c71ba545fbde06d99a9d31e8140 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/untis/EecAndDec.js" @@ -0,0 +1,15 @@ +let crypto = require("crypto"); + +let EncAndDec = { + + md5: function (data) { + let md5 = crypto.createHash("md5"); + + md5.update(data); + + return md5.digest("hex"); + + } +} + +module.exports=EncAndDec; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/yiqing.sql" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/yiqing.sql" new file mode 100644 index 0000000000000000000000000000000000000000..0fa5e6d57bee7a589797af90d65152cd061b5ef8 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/yiqing.sql" @@ -0,0 +1,52 @@ +/* +Navicat MySQL Data Transfer + +Source Server : loclahost +Source Server Version : 50553 +Source Host : localhost:3306 +Source Database : yiqing + +Target Server Type : MYSQL +Target Server Version : 50553 +File Encoding : 65001 + +Date: 2022-03-15 16:10:32 +*/ + +SET FOREIGN_KEY_CHECKS=0; + +-- ---------------------------- +-- Table structure for number +-- ---------------------------- +DROP TABLE IF EXISTS `number`; +CREATE TABLE `number` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `nums` smallint(6) DEFAULT NULL, + `date` varchar(255) DEFAULT NULL, + `city` varchar(64) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; + +-- ---------------------------- +-- Records of number +-- ---------------------------- +INSERT INTO `number` VALUES ('1', '0', '2022-3-15', '龙岩'); +INSERT INTO `number` VALUES ('2', '3', '2022-3-15', '厦门'); +INSERT INTO `number` VALUES ('3', '0', '2022-3-14', '龙岩'); +INSERT INTO `number` VALUES ('4', '0', '2022-3-14', '厦门'); + +-- ---------------------------- +-- Table structure for user +-- ---------------------------- +DROP TABLE IF EXISTS `user`; +CREATE TABLE `user` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(64) DEFAULT NULL, + `password` varchar(32) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; + +-- ---------------------------- +-- Records of user +-- ---------------------------- +INSERT INTO `user` VALUES ('1', 'user', '123456'); diff --git "a/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/yiqing1.html" "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/yiqing1.html" new file mode 100644 index 0000000000000000000000000000000000000000..2eff46a65676e4647e04904ee004349a2233c544 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220325_\347\210\254\345\217\226\346\225\260\346\215\256requestsina\347\226\253\346\203\205/yiqing1.html" @@ -0,0 +1,661 @@ + + + + + + + + + + + + + 实时更新:新型冠状病毒肺炎疫情地图 + + + + + + + + + + + + + + + + +
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file