diff --git "a/\350\265\226\350\231\271\351\234\226/20220310\350\207\252\345\273\272\346\241\206\346\236\266/china.html" "b/\350\265\226\350\231\271\351\234\226/20220310\350\207\252\345\273\272\346\241\206\346\236\266/china.html" new file mode 100644 index 0000000000000000000000000000000000000000..614ea27bddb82ad17607884429c7d73439c69220 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220310\350\207\252\345\273\272\346\241\206\346\236\266/china.html" @@ -0,0 +1,27 @@ + + + + + 表格实例 + + + + + + + + + + + + + + + + + {{result}} + +
全国疫情分布
城市数量日期
+ + + \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220310\350\207\252\345\273\272\346\241\206\346\236\266/situation.sql" "b/\350\265\226\350\231\271\351\234\226/20220310\350\207\252\345\273\272\346\241\206\346\236\266/situation.sql" new file mode 100644 index 0000000000000000000000000000000000000000..09112ef43bd8ea409805e9914455d330599c077d --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220310\350\207\252\345\273\272\346\241\206\346\236\266/situation.sql" @@ -0,0 +1,34 @@ +/* +Navicat MySQL Data Transfer + +Source Server : localhost +Source Server Version : 50553 +Source Host : localhost:3306 +Source Database : situation + +Target Server Type : MYSQL +Target Server Version : 50553 +File Encoding : 65001 + +Date: 2022-03-10 15:44:59 +*/ + +SET FOREIGN_KEY_CHECKS=0; + +-- ---------------------------- +-- Table structure for china +-- ---------------------------- +DROP TABLE IF EXISTS `china`; +CREATE TABLE `china` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `city` varchar(64) DEFAULT NULL, + `nums` tinyint(4) DEFAULT NULL, + `date` varchar(64) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8; + +-- ---------------------------- +-- Records of china +-- ---------------------------- +INSERT INTO `china` VALUES ('1', '龙岩', '0', '2022-03-10'); +INSERT INTO `china` VALUES ('2', '厦门', '0', '2022-03-10'); diff --git "a/\350\265\226\350\231\271\351\234\226/20220310\350\207\252\345\273\272\346\241\206\346\236\266/\345\212\250\346\200\201\347\275\221\347\253\231demo.js" "b/\350\265\226\350\231\271\351\234\226/20220310\350\207\252\345\273\272\346\241\206\346\236\266/\345\212\250\346\200\201\347\275\221\347\253\231demo.js" new file mode 100644 index 0000000000000000000000000000000000000000..f4ab2029f24f4f985aaa7dc9725aec5a94ed4bf7 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220310\350\207\252\345\273\272\346\241\206\346\236\266/\345\212\250\346\200\201\347\275\221\347\253\231demo.js" @@ -0,0 +1,31 @@ + +let http = require('http'); +let mysql = require('mysql'); +let fs = require('fs'); +http.createServer(function(req,res){ + if(req.url=='/china'){ + res.setHeader('Content-Type', 'text/html; charset=utf-8'); + let connection = mysql.createConnection({host:'localhost',user:'root',password:'123456',port:'3306',database:'situation'}); + connection.connect(); + connection.query("select * from china where `date`=?",'2022-03-10',function(err,rows){ + let html='' + for(let row in rows){ + html+='' + html+=''+ rows[row].city+'' + html+=''+ rows[row].nums+'' + html+=''+ rows[row].date+'' + html+='' + } + console.log(html); + fs.readFile('./china.html',function(err,buffer){ + let outPutStr = buffer.toString(); + let newStr= outPutStr.replace('{{result}}',html); + res.write(newStr); + res.end(); + }); + }); + }else{ + res.end(); + } +}).listen(5500) +console.log(" http://localhost:5500/china"); diff --git "a/\350\265\226\350\231\271\351\234\226/20220311\351\235\231\346\200\201\350\265\204\346\272\220/app.js" "b/\350\265\226\350\231\271\351\234\226/20220311\351\235\231\346\200\201\350\265\204\346\272\220/app.js" new file mode 100644 index 0000000000000000000000000000000000000000..28ee963b8835615d97ddbb472f7e976ba6303bd8 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220311\351\235\231\346\200\201\350\265\204\346\272\220/app.js" @@ -0,0 +1,33 @@ +let http = require("http"); +let fs = require("fs"); +let server = http.createServer(); +server.listen(8080); +console.log(" http://localhost:8080"); +server.on('request', function (req, res) { + res.setHeader("Content-Type", " charset=utf-8"); + let url = req.url; + if (url.lastIndexOf('.') > -1) { + let path = '.' + url; + readFileByPromise(path,res); + } else { + res.end( '404'); + } +}) +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); + res.write(data); + res.end(); +} + diff --git "a/\350\265\226\350\231\271\351\234\226/20220311\351\235\231\346\200\201\350\265\204\346\272\220/home/imgs/404.png" "b/\350\265\226\350\231\271\351\234\226/20220311\351\235\231\346\200\201\350\265\204\346\272\220/home/imgs/404.png" new file mode 100644 index 0000000000000000000000000000000000000000..c7166a41c552008bb32ad37bf2500571afd4841e Binary files /dev/null and "b/\350\265\226\350\231\271\351\234\226/20220311\351\235\231\346\200\201\350\265\204\346\272\220/home/imgs/404.png" differ diff --git "a/\350\265\226\350\231\271\351\234\226/20220315url\345\212\250\346\200\201\350\265\204\346\272\220/.keep" "b/\350\265\226\350\231\271\351\234\226/20220315url\345\212\250\346\200\201\350\265\204\346\272\220/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220315url\345\212\250\346\200\201\350\265\204\346\272\220/home/.keep" "b/\350\265\226\350\231\271\351\234\226/20220315url\345\212\250\346\200\201\350\265\204\346\272\220/home/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220315url\345\212\250\346\200\201\350\265\204\346\272\220/home/controller/.keep" "b/\350\265\226\350\231\271\351\234\226/20220315url\345\212\250\346\200\201\350\265\204\346\272\220/home/controller/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220315url\345\212\250\346\200\201\350\265\204\346\272\220/home/controller/index.js" "b/\350\265\226\350\231\271\351\234\226/20220315url\345\212\250\346\200\201\350\265\204\346\272\220/home/controller/index.js" new file mode 100644 index 0000000000000000000000000000000000000000..06ff72b438c5cfeceba443172520b215cf8c4b77 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220315url\345\212\250\346\200\201\350\265\204\346\272\220/home/controller/index.js" @@ -0,0 +1,16 @@ + +let index={ + index:function(req,res){ + let mysql = require("../../model/base"); + let date = req.query['date']; + let sql = 'select * from number where date=?'; + mysql.query(sql,date,res); + }, + show:function(){ + console.log("今天很热.................."); + } +} + +module.exports=index; + +// \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220315url\345\212\250\346\200\201\350\265\204\346\272\220/home/html/.keep" "b/\350\265\226\350\231\271\351\234\226/20220315url\345\212\250\346\200\201\350\265\204\346\272\220/home/html/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220315url\345\212\250\346\200\201\350\265\204\346\272\220/home/html/index.html" "b/\350\265\226\350\231\271\351\234\226/20220315url\345\212\250\346\200\201\350\265\204\346\272\220/home/html/index.html" new file mode 100644 index 0000000000000000000000000000000000000000..3bfade0fe39777dae1cf230c317a882692d3f3f4 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220315url\345\212\250\346\200\201\350\265\204\346\272\220/home/html/index.html" @@ -0,0 +1,26 @@ + + + + + Bootstrap 实例 - 基本的表格 + + + + + + + + + + + + + + + + {{result}} + +
名称城市
+ + + \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220315url\345\212\250\346\200\201\350\265\204\346\272\220/model/.keep" "b/\350\265\226\350\231\271\351\234\226/20220315url\345\212\250\346\200\201\350\265\204\346\272\220/model/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220315url\345\212\250\346\200\201\350\265\204\346\272\220/model/base.js" "b/\350\265\226\350\231\271\351\234\226/20220315url\345\212\250\346\200\201\350\265\204\346\272\220/model/base.js" new file mode 100644 index 0000000000000000000000000000000000000000..8f0254294d4543f40de0d3f4d04cc5efa545563f --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220315url\345\212\250\346\200\201\350\265\204\346\272\220/model/base.js" @@ -0,0 +1,31 @@ + +let fs = require("fs"); +let mysql = { + query: function (sql, value, res) { + let mysql = require("mysql"); + let connection = mysql.createConnection({ host: '127.0.0.1', user: 'root', password: '123456', database: 'yiqing' }); + connection.connect(); + connection.query(sql, value, function (err, 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); + connection.end(); + res.write(formatHtml); + res.end(); + + }) + + }) + + } +} + +module.exports = mysql; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220315url\345\212\250\346\200\201\350\265\204\346\272\220/url\345\212\250\346\200\201\350\265\204\346\272\220.js" "b/\350\265\226\350\231\271\351\234\226/20220315url\345\212\250\346\200\201\350\265\204\346\272\220/url\345\212\250\346\200\201\350\265\204\346\272\220.js" new file mode 100644 index 0000000000000000000000000000000000000000..b134d37befba9c221b19f158b8848ca0d4f08f23 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220315url\345\212\250\346\200\201\350\265\204\346\272\220/url\345\212\250\346\200\201\350\265\204\346\272\220.js" @@ -0,0 +1,32 @@ +let http = require("http"); +let fs = require("fs"); +let server = http.createServer(); +server.listen(8080); +console.log(" http://localhost:8080/?a=index&c=index"); +server.on('request', function (req, res) { + let url = req.url; + if (url.lastIndexOf('.') > -1) { + } + else { + res.setHeader("Content-type","text/html;charset=utf8"); + let queryString = url.split("?")[1]; + let queryArr = queryString.split("&"); + + let keyVals = []; + for (let key in queryArr) { + let vals = queryArr[key]; + let temArray = vals.split("="); + keyVals[temArray[0]] = temArray[1]; + } + req.query = keyVals; + let controllerPath = './home/controller/' + keyVals['c']; + let controller = require(controllerPath); + let show = keyVals['a']; + console.log(controller[show](req,res)); + + } + res.write( '点击获取20220314的数据' ); + res.write('点击获取20220315的数据') + +}) + diff --git "a/\350\265\226\350\231\271\351\234\226/20220316call\344\274\230\345\214\226/home/.keep" "b/\350\265\226\350\231\271\351\234\226/20220316call\344\274\230\345\214\226/home/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220316call\344\274\230\345\214\226/home/controller/.keep" "b/\350\265\226\350\231\271\351\234\226/20220316call\344\274\230\345\214\226/home/controller/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220316call\344\274\230\345\214\226/home/controller/index.js" "b/\350\265\226\350\231\271\351\234\226/20220316call\344\274\230\345\214\226/home/controller/index.js" new file mode 100644 index 0000000000000000000000000000000000000000..fb69ae86016b681a654ce9e296d5e5e183c54bbd --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220316call\344\274\230\345\214\226/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/20220316call\344\274\230\345\214\226/home/html/index.html" "b/\350\265\226\350\231\271\351\234\226/20220316call\344\274\230\345\214\226/home/html/index.html" new file mode 100644 index 0000000000000000000000000000000000000000..09a3075dc79a1e0edf5ab4b4a39013da6c771305 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220316call\344\274\230\345\214\226/home/html/index.html" @@ -0,0 +1,24 @@ + + + + + Bootstrap 实例 - 基本的表格 + + + + + + + + + + + + + + + {{result}} + +
名称城市
+ + \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220316call\344\274\230\345\214\226/home/imgs/404.png" "b/\350\265\226\350\231\271\351\234\226/20220316call\344\274\230\345\214\226/home/imgs/404.png" new file mode 100644 index 0000000000000000000000000000000000000000..c7166a41c552008bb32ad37bf2500571afd4841e Binary files /dev/null and "b/\350\265\226\350\231\271\351\234\226/20220316call\344\274\230\345\214\226/home/imgs/404.png" differ diff --git "a/\350\265\226\350\231\271\351\234\226/20220316call\344\274\230\345\214\226/model/.keep" "b/\350\265\226\350\231\271\351\234\226/20220316call\344\274\230\345\214\226/model/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220316call\344\274\230\345\214\226/model/base.js" "b/\350\265\226\350\231\271\351\234\226/20220316call\344\274\230\345\214\226/model/base.js" new file mode 100644 index 0000000000000000000000000000000000000000..04e5476ba45cf14ee0641ff1afbe0c0153cb6ffb --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220316call\344\274\230\345\214\226/model/base.js" @@ -0,0 +1,88 @@ + +let fs = require("fs"); + +let mysqlModule = require("mysql"); + +let connection = null; + +let mysql = { + + + init: function demo() { + connection = mysqlModule.createConnection({ host: '127.0.0.1', user: 'root', password: '123456', database: 'yiqing' }); + + }, + + unuseQuery: function (sql, value, res) { + + + mysql.init() + connection.connect(); + + connection.query(sql, value, function (err, result) { + 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); + connection.end(); + res.write(formatHtml); + res.end(); + + }) + + }) + + }, + + query: function name(sql, value) { + + let promise = new Promise(function (resolve, reject) { + + mysql.init(); + connection.connect(); + /** + * 推荐使用占位符方式查询,占位符可以防止 sql注入 占了 80%漏洞都是sql + */ + connection.query(sql, value, function (err, result) { + + if (err) { + console.log(err.message); + //reject(err.message) + } else { + + //setTimeout(function () {//模拟数据的慢查询 + resolve(result); + //},5000) + + } + + connection.end(); + }) + + + }) + return promise; + + } + + + + + + + +} + +module.exports = mysql; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220316call\344\274\230\345\214\226/url\345\212\250\346\200\201\350\265\204\346\272\220.js" "b/\350\265\226\350\231\271\351\234\226/20220316call\344\274\230\345\214\226/url\345\212\250\346\200\201\350\265\204\346\272\220.js" new file mode 100644 index 0000000000000000000000000000000000000000..e49927bfa4395fc700f6d842bb0952330c42657a --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220316call\344\274\230\345\214\226/url\345\212\250\346\200\201\350\265\204\346\272\220.js" @@ -0,0 +1,57 @@ + +let http = require("http"); +let fs = require("fs"); +let server = http.createServer(); +server.listen(8080); +console.log("http://localhost:8080/index.html"); + +console.log("http://localhost:8080/?a=index&c=index&date=2022-3-15"); + +server.on('request', function (req, res) { + let url = req.url; + if (url.lastIndexOf('.') > -1) { + 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; + let controllerPath = './home/controller/' + keyVals['c']; + let controller = require(controllerPath); + let show = keyVals['a']; + console.log(controller[show](req,res)); + // + + + } + +}) +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/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/home/.keep" "b/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/home/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/home/controller/.keep" "b/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/home/controller/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/home/controller/BaseController.js" "b/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/home/controller/BaseController.js" new file mode 100644 index 0000000000000000000000000000000000000000..b93bfdf2834fb59577e5fc8088b5a6fde0bb2b80 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/home/controller/BaseController.js" @@ -0,0 +1,17 @@ + +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/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/home/controller/IndexController.js" "b/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/home/controller/IndexController.js" new file mode 100644 index 0000000000000000000000000000000000000000..9c0d36d0469925a742ad027b2f1a68776bee1a97 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/home/controller/IndexController.js" @@ -0,0 +1,31 @@ +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); + return this.display('index.html', { data: result }) + } 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); + return this.display('detail.html', { data: result }) + } catch (err) { + console.log(err); + } + } +} +module.exports = IndexController; diff --git "a/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/home/html/.keep" "b/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/home/html/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/home/html/detail.html" "b/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/home/html/detail.html" new file mode 100644 index 0000000000000000000000000000000000000000..310008110a0d40ae751884004f3aba4f4ec8e097 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/home/html/detail.html" @@ -0,0 +1,22 @@ + + + + + + + + <detail页面 + + + + +

各城市疫情

+ {% for item in data %} + + {{ item.city }} + {{ item.nums }} + + {% endfor %} + + + \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/home/html/index.html" "b/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/home/html/index.html" new file mode 100644 index 0000000000000000000000000000000000000000..67a7bc6533ee3baa35de68687cda68e1dc61a352 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/home/html/index.html" @@ -0,0 +1,78 @@ + + + + + + + + index页面 + + + + + +
+ +

疫情实时大数据报告

+ + +
+

疫情速报

+ + + + + + + + + {% for item in data %} + + + + + {% endfor %} +

地区

新增本土

现有病例

{{ item.city }}{{ item.nums }}
+
+ + +
+ + + + + + \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/model/.keep" "b/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/model/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/model/base.js" "b/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/model/base.js" new file mode 100644 index 0000000000000000000000000000000000000000..2fe25c8231e5aaa16ad481c91385ec6e54a5d014 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/model/base.js" @@ -0,0 +1,22 @@ + +let mysqlModule = require("mysql"); +let connection = null; +class mysql { + query(sql, value) { + let promise = new Promise(function (resolve, reject) { + let connection = mysqlModule.createConnection({ host: '127.0.0.1', user: 'root', password: '123456', database: 'yiqing' }); + connection.connect(); + + connection.query(sql, value, function (err, result) { + if (err) { + console.log(err.message); + } else { + resolve(result); + } + connection.end(); + }) + }) + return promise; + } +} +module.exports = mysql; \ No newline at end of file diff --git "a/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/url\345\212\250\346\200\201\350\265\204\346\272\220.js" "b/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/url\345\212\250\346\200\201\350\265\204\346\272\220.js" new file mode 100644 index 0000000000000000000000000000000000000000..96f4ae641b622755f801a7b21cfef15727ae2722 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220317\345\274\225\346\223\216\346\250\241\346\235\277\344\274\230\345\214\226/url\345\212\250\346\200\201\350\265\204\346\272\220.js" @@ -0,0 +1,67 @@ + +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=index&c=index&date=2022-3-15"); +console.log(" http://localhost:8080/?a=detail&c=index&date=2022-3-14"); + + +server.on('request', async function (req, res) { + let url = req.url; + + if (url.lastIndexOf('.') > -1) { + let path = '.' + url; + readFileByPromise(path, res); + } else { + res.setHeader("Content-type", "text/html;charset=utf8"); + let queryString = url.split("?")[1]; + let queryArr = queryString.split("&"); + let keyVals = []; + for (let key in queryArr) { + let vals = queryArr[key]; + let temArray = vals.split("="); + keyVals[temArray[0]] = temArray[1]; + } + //给 req 对象添加一个 query + 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.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/20220318\351\241\271\347\233\256\344\274\230\345\214\226/config/db.js" "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/config/db.js" new file mode 100644 index 0000000000000000000000000000000000000000..e1b143475ee09e95e6de0637d7af3fac6d05c55e --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/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/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/.keep" "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/controller/.keep" "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/controller/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/controller/BaseController.js" "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/controller/BaseController.js" new file mode 100644 index 0000000000000000000000000000000000000000..66d265e4dbefd72703b5274ae8b6b275183c87ad --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/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/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/controller/IndexController.js" "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/controller/IndexController.js" new file mode 100644 index 0000000000000000000000000000000000000000..57d9af51225f4934757ba33f17176b678f6958aa --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/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/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/controller/UserController.js" "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/controller/UserController.js" new file mode 100644 index 0000000000000000000000000000000000000000..c34e141b3f2ec3f5e855af1ba8e72ab2407b76d0 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/controller/UserController.js" @@ -0,0 +1,45 @@ +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/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/controller/index.js" "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/controller/index.js" new file mode 100644 index 0000000000000000000000000000000000000000..fb69ae86016b681a654ce9e296d5e5e183c54bbd --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/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/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/html/.keep" "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/html/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/html/detail.html" "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/html/detail.html" new file mode 100644 index 0000000000000000000000000000000000000000..f6fbfa950367b48ccdd8dcc5e64b93c866cd2c53 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/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/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/html/dlu.html" "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/html/dlu.html" new file mode 100644 index 0000000000000000000000000000000000000000..72d0259cce84ed43935a26ec4ca48b8c33f28841 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/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/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/html/footer.html" "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/html/footer.html" new file mode 100644 index 0000000000000000000000000000000000000000..d619210ac5cfa2dd8f909c0a035f7a6128f9c6b0 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/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/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/html/header.html" "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/html/header.html" new file mode 100644 index 0000000000000000000000000000000000000000..349ba5b9820c1d4ece42fd6fcdd457553b90c183 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/html/header.html" @@ -0,0 +1,10 @@ + + + + + + + + {{title}} + + diff --git "a/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/html/index.html" "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/html/index.html" new file mode 100644 index 0000000000000000000000000000000000000000..d5451621b464070fcf157d390e059b992e1f89a0 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/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/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/html/login.html" "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/html/login.html" new file mode 100644 index 0000000000000000000000000000000000000000..91a7a097413d6ed41e7518a2e657545b91b122ff --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/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/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/html/userReg.html" "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/home/html/userReg.html" new file mode 100644 index 0000000000000000000000000000000000000000..ccd016e524733d38c6d288f5d4368cbc200c0a6d --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/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/20220318\351\241\271\347\233\256\344\274\230\345\214\226/model/.keep" "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/model/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/model/Base.js" "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/model/Base.js" new file mode 100644 index 0000000000000000000000000000000000000000..937befcfe89b8956fe17e0c8286574e16469edca --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/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/20220318\351\241\271\347\233\256\344\274\230\345\214\226/model/UserModel.js" "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/model/UserModel.js" new file mode 100644 index 0000000000000000000000000000000000000000..f5b5c67efca3f38c5003c5d62a44877509c04256 --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/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/20220318\351\241\271\347\233\256\344\274\230\345\214\226/url\345\212\250\346\200\201\350\265\204\346\272\220.js" "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/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/20220318\351\241\271\347\233\256\344\274\230\345\214\226/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/20220318\351\241\271\347\233\256\344\274\230\345\214\226/yiqing.sql" "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/yiqing.sql" new file mode 100644 index 0000000000000000000000000000000000000000..05b9949deda7889a6495fe6e73f2b62a0713dd8e --- /dev/null +++ "b/\350\265\226\350\231\271\351\234\226/20220318\351\241\271\347\233\256\344\274\230\345\214\226/yiqing.sql" @@ -0,0 +1,59 @@ +/* + Navicat Premium Data Transfer + + Source Server : localhost + Source Server Type : MySQL + Source Server Version : 80027 + Source Host : localhost:3306 + Source Schema : yiqing + + Target Server Type : MySQL + Target Server Version : 80027 + File Encoding : 65001 + + Date: 25/03/2022 23:04:04 +*/ + +SET NAMES utf8mb4; +SET FOREIGN_KEY_CHECKS = 0; + +-- ---------------------------- +-- Table structure for number +-- ---------------------------- +DROP TABLE IF EXISTS `number`; +CREATE TABLE `number` ( + `id` int NOT NULL AUTO_INCREMENT, + `nums` smallint NULL DEFAULT NULL, + `date` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, + `city` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, + PRIMARY KEY (`id`) USING BTREE +) ENGINE = MyISAM AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; + +-- ---------------------------- +-- 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 NOT NULL AUTO_INCREMENT, + `name` varchar(64) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, + `password` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, + PRIMARY KEY (`id`) USING BTREE +) ENGINE = MyISAM AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; + +-- ---------------------------- +-- Records of user +-- ---------------------------- +INSERT INTO `user` VALUES (1, 'user', '123456'); +INSERT INTO `user` VALUES (2, 'asdasd', 'a8f5f167f44f4964e6c998dee827110c'); +INSERT INTO `user` VALUES (3, '%E9%BB%84', 'a8f5f167f44f4964e6c998dee827110c'); +INSERT INTO `user` VALUES (4, '%E9%BB%84', 'a8f5f167f44f4964e6c998dee827110c'); + +SET FOREIGN_KEY_CHECKS = 1;