HtmlServer.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. var express = require('express');
  2. var app = express();
  3. //require('./clienttmp.js')
  4. const queryString = require("querystring")
  5. //var MongoClient = require('mongodb').MongoClient;
  6. //var dburl = "mongodb://localhost:27017/vr";
  7. var mysql = require('mysql');
  8. var connection = mysql.createConnection({
  9. host : 'localhost',
  10. user : 'root',
  11. password : 'HS1205',
  12. database : 'vr'
  13. });
  14. connection.connect();
  15. var session = require("express-session");
  16. var cookie = require("cookie-parser");
  17. app.use(cookie());
  18. app.use(session({
  19. secret: 'secret', // 对session id 相关的cookie 进行签名
  20. saveUninitialized: true, // 是否保存未初始化的会话
  21. cookie: { maxAge: 1000 * 60 * 5 }, //过期时间 毫秒为单位
  22. resave: true, //每次触发后保存时间
  23. rolling: true, // 最后一次触发后计时
  24. }));
  25. app.use(function (req, res, next) {
  26. var user = req.session.user;
  27. console.dir(user);
  28. res.locals.user = req.session.user;
  29. var err = req.session.error;
  30. res.locals.message = '';
  31. if (err) res.locals.message = '<div style="margin-bottom: 20px;color:red;">' + err + '</div>';
  32. //next();
  33. console.log(req.path);
  34. //path模块
  35. var path = require('path'); /*nodejs自带的模块*/
  36. var extname = path.extname(req.path); //获取文件的后缀名
  37. console.log(extname);
  38. var skip = 0;
  39. if (extname != '.html') {
  40. skip = 1;
  41. }
  42. if (req.path == 'login.html') {
  43. skip = 1;
  44. }
  45. if (skip) {
  46. next();
  47. return;
  48. }
  49. if (req.path != '/' && !req.session.user)//|| (req.session.user && extname !='.html')))//不是主页 而且没登录或者登录后后缀不是.html的
  50. {
  51. // fs.readFile('./practice/login.html', 'utf-8', function (err, data) {
  52. // if (err) {
  53. // throw err;
  54. // }
  55. // res.send(data);
  56. // })
  57. //res.redirect(302, '/');
  58. //window.open('Login.html','_top')
  59. res.send("<script>window.open('/','_top');</script>")
  60. }
  61. else {
  62. next();
  63. }
  64. });
  65. var path = require('path');
  66. app.set('views', path.join(__dirname, 'views'));
  67. app.set('view engine', 'ejs');
  68. var con = require('./conn.js');
  69. const url = require("url")
  70. var fs = require('fs');
  71. app.use(express.static(path.join(__dirname, 'css')));
  72. app.use(express.static(path.join(__dirname, 'practice')));
  73. app.use(express.static(path.join(__dirname, 'node_modules')));
  74. app.use(express.static(path.join(__dirname, 'img')));
  75. app.get('/', function (req, res) {
  76. console.log("主页 GET 请求");
  77. //console.log(req.cookies.user);
  78. var user = req.session.user;
  79. console.dir(user);
  80. if (req.session.user) {
  81. fs.readFile('./views/index.html', 'utf-8', function (err, data) {
  82. if (err) {
  83. throw err;
  84. }
  85. res.send(data);
  86. })
  87. }
  88. else {
  89. fs.readFile('./practice/login.html', 'utf-8', function (err, data) {
  90. if (err) {
  91. throw err;
  92. }
  93. res.send(data);
  94. })
  95. }
  96. })
  97. app.get('/logout', function (req, res) {
  98. console.log("logout")
  99. console.log("注销用户")
  100. req.session.destroy();
  101. fs.readFile('./practice/login.html', 'utf-8', function (err, data) {
  102. if (err) {
  103. throw err;
  104. }
  105. res.send(data);
  106. })
  107. })
  108. // POST 请求
  109. /*
  110. app.post('/', function (req, res) {
  111. console.log("主页 POST 请求");
  112. req.rawBody = '';
  113. var json = {};
  114. req.setEncoding('utf8');
  115. req.on('data', function (chunk) {
  116. req.rawBody += chunk;
  117. });
  118. req.on('end', function () {
  119. //json=JSON.parse(req.rawBody);
  120. //res.send(JSON.stringify(json));
  121. res.send(req.rawBody);
  122. });
  123. })
  124. */
  125. app.post('/', function (req, res) {
  126. console.log("主页 POST 请求");
  127. req.rawBody = '';
  128. req.setEncoding('utf8');
  129. req.on('data', function (chunk) {
  130. req.rawBody += chunk;
  131. });
  132. req.on('end', function () {
  133. req.rawBody = queryString.parse(req.rawBody)
  134. var body = req.rawBody;
  135. //res.send(JSON.stringify(json));
  136. var a = body.acc;
  137. var b = body.pwd;
  138. //console.log(req.cookies.user);
  139. var user = req.session.user;
  140. console.dir(user);
  141. if (a == "admin" && b == "hsvr113" || a == "hs" && b == "111") {
  142. req.cookies.username = a;
  143. req.session.user = a;
  144. fs.readFile('./views/index.html', 'utf-8', function (err, data) {
  145. if (err) {
  146. throw err;
  147. }
  148. res.send(data);
  149. })
  150. }
  151. else {
  152. //res.send("账户或密码错误!")
  153. fs.readFile('./practice/login.html', 'utf-8', function (err, data) {
  154. if (err) {
  155. throw err;
  156. }
  157. res.send(data);
  158. })
  159. }
  160. });
  161. })
  162. app.post('/dataquery', function (req, res) {
  163. console.log("dataquery POST 请求");
  164. req.rawBody = '';
  165. var json = {};
  166. req.setEncoding('utf8');
  167. req.on('data', function (chunk) {
  168. req.rawBody += chunk;
  169. });
  170. req.on('end', function () {
  171. req.rawBody = queryString.parse(req.rawBody)
  172. var body = req.rawBody;
  173. var type = body.type;
  174. var userid = body.userid;
  175. var begdate = body.begdate;
  176. begdate = begdate.replace(/T/, " ") + ":00";
  177. var enddate = body.enddate;
  178. enddate = enddate.replace(/T/, " ") + ":59";
  179. var whereStr = { "userid": userid, "monitorTime": { "$gte": begdate, "$lte": enddate } };
  180. if (!userid) {
  181. whereStr = {};
  182. }
  183. var jsObj = {};
  184. if (userid) {
  185. jsObj.userid = userid;
  186. }
  187. jsObj.monitorTime = { "$gte": begdate, "$lte": enddate };
  188. var str = JSON.stringify(jsObj);
  189. console.log(typeof str);
  190. var str1 = JSON.parse(str);
  191. console.log(typeof str1);
  192. var mysort = { monitorTime: -1 };
  193. if (type == 'record') {
  194. var sql = 'SELECT * FROM t_records';
  195. //查
  196. connection.query(sql, function (err, result) {
  197. if (err) {
  198. console.log('[SELECT ERROR] - ', err.message);
  199. return;
  200. }
  201. console.log('--------------------------SELECT----------------------------');
  202. console.log(result);
  203. console.log('------------------------------------------------------------\n\n');
  204. res.send(result);
  205. });
  206. }
  207. /*
  208. MongoClient.connect(dburl, { useNewUrlParser: true }, function (err, db) {
  209. if (err) throw err;
  210. var dbo = db.db("vr");
  211. var setname;
  212. if (type == 'record') {
  213. setname = 'record';
  214. }
  215. else if (type == 'envir') {
  216. setname = 'envir';
  217. }
  218. else if (type == 'warning') {
  219. setname = 'warning';
  220. }
  221. else if (type == 'config') {
  222. setname = 'config';
  223. }
  224. if (setname) {
  225. //if (setname != 'envir') {
  226. dbo.collection(setname).find(str1).sort(mysort).toArray(function (err, result) { // 返回集合中所有数据
  227. if (err) throw err;
  228. //console.log(result);
  229. db.close();
  230. console.log('查询数据库返回');
  231. res.send(result)
  232. });
  233. // }
  234. // else {
  235. // dbo.collection(setname).find(str1).toArray(function (err, result) { // 返回集合中所有数据
  236. // if (err) throw err;
  237. // //console.log(result);
  238. // db.close();
  239. // console.log('查询数据库返回');
  240. // res.send(result)
  241. // });
  242. // }
  243. }
  244. });
  245. */
  246. });
  247. })
  248. app.post('/getuser', function (req, res) {
  249. console.log("getuser POST 请求");
  250. req.rawBody = '';
  251. var json = {};
  252. req.setEncoding('utf8');
  253. req.on('data', function (chunk) {
  254. req.rawBody += chunk;
  255. });
  256. req.on('end', function () {
  257. var a = req.rawBody
  258. res.send(req.session.user);
  259. })
  260. })
  261. //模板
  262. app.post('/dbinterface/setname', function (req, res) {
  263. console.log(" dbinterface setname POST 请求");
  264. req.rawBody = '';
  265. var json = {};
  266. req.setEncoding('utf8');
  267. req.on('data', function (chunk) {
  268. req.rawBody += chunk;
  269. });
  270. req.on('end', function () {
  271. var body = req.rawBody
  272. var action = body.action;
  273. if (action == 'get') {
  274. }
  275. else if (action == 'add') {
  276. }
  277. else if (action == 'update') {
  278. }
  279. else if (action == 'delete') {
  280. }
  281. })
  282. })
  283. app.post('/dbinterface/stationinfo', function (req, res) {
  284. req.rawBody = '';
  285. var json = {};
  286. req.setEncoding('utf8');
  287. req.on('data', function (chunk) {
  288. req.rawBody += chunk;
  289. });
  290. req.on('end', function () {
  291. var body = JSON.parse(req.rawBody);
  292. var action = body.action;
  293. console.log(req.path + " " + action + " 请求");
  294. if (action == 'get') {
  295. /*
  296. MongoClient.connect(dburl, { useNewUrlParser: true }, function (err, db) {
  297. if (err) throw err;
  298. var dbo = db.db("vr");
  299. dbo.collection('stationinfo').find({}).toArray(function (err, result) {
  300. if (err) throw err;
  301. console.log(result);
  302. db.close();
  303. res.send(result)
  304. });
  305. });
  306. */
  307. }
  308. else if (action == 'add') {
  309. /*
  310. MongoClient.connect(dburl, { useNewUrlParser: true }, function (err, db) {
  311. if (err) throw err;
  312. var dbo = db.db("vr");
  313. var myobj = body.info;
  314. dbo.collection("stationinfo").insertOne(myobj, function (err, res) {
  315. if (err) throw err;
  316. console.log("站点信息插入成功");
  317. db.close();
  318. });
  319. });
  320. */
  321. }
  322. else if (action == 'update') {
  323. }
  324. else if (action == 'delete') {
  325. }
  326. })
  327. })
  328. //api 模板
  329. app.post('/api/get', function (req, res) {
  330. console.log(req.path + " 请求");
  331. req.rawBody = '';
  332. req.setEncoding('utf8');
  333. req.on('data', function (chunk) {
  334. req.rawBody += chunk;
  335. });
  336. req.on('end', function () {
  337. var body = JSON.parse(req.rawBody);
  338. var json = {};
  339. json['code'] = 200;
  340. json['message'] = "请求成功"
  341. json['success'] = 'true'
  342. json['data'] = {};
  343. res.send(json);
  344. })
  345. })
  346. app.post('/api/getallstation', function (req, res) {
  347. console.log(req.path + " 请求");
  348. req.rawBody = '';
  349. req.setEncoding('utf8');
  350. req.on('data', function (chunk) {
  351. req.rawBody += chunk;
  352. });
  353. req.on('end', function () {
  354. var body = JSON.parse(req.rawBody);
  355. var json = {};
  356. json['code'] = 200;
  357. json['massage'] = "请求成功"
  358. /*
  359. MongoClient.connect(dburl, { useNewUrlParser: true }, function (err, db) {
  360. if (err) throw err;
  361. var dbo = db.db("vr");
  362. dbo.collection('stationinfo').find({}).toArray(function (err, result) {
  363. if (err) throw err;
  364. //console.log(result);
  365. db.close();
  366. json['success'] = 'true'
  367. json['data'] = result;
  368. res.send(json)
  369. });
  370. });
  371. */
  372. })
  373. })
  374. app.post('/api/getstation/:id', function (req, res) {
  375. console.log(req.path + " 请求");
  376. req.rawBody = '';
  377. req.setEncoding('utf8');
  378. req.on('data', function (chunk) {
  379. req.rawBody += chunk;
  380. });
  381. req.on('end', function () {
  382. var body = JSON.parse(req.rawBody);
  383. var json = {};
  384. json['code'] = 200;
  385. json['massage'] = "请求成功"
  386. var id = req.params.id;
  387. var whereStr = { "id": id }
  388. /*
  389. MongoClient.connect(dburl, { useNewUrlParser: true }, function (err, db) {
  390. if (err) throw err;
  391. var dbo = db.db("vr");
  392. dbo.collection('stationinfo').find(whereStr).toArray(function (err, result) {
  393. if (err) throw err;
  394. console.log(result);
  395. db.close();
  396. json['success'] = 'true'
  397. json['data'] = result;
  398. res.send(json)
  399. });
  400. });
  401. */
  402. })
  403. })
  404. app.post('/api/addstation', function (req, res) {
  405. console.log(req.path + " 请求");
  406. req.rawBody = '';
  407. req.setEncoding('utf8');
  408. req.on('data', function (chunk) {
  409. req.rawBody += chunk;
  410. });
  411. req.on('end', function () {
  412. var body = JSON.parse(req.rawBody);
  413. var json = {};
  414. json['code'] = 200;
  415. json['message'] = "请求成功"
  416. /*
  417. MongoClient.connect(dburl, { useNewUrlParser: true }, function (err, db) {
  418. if (err) throw err;
  419. var dbo = db.db("vr");
  420. var myobj = body.data;
  421. dbo.collection("stationinfo").insertOne(myobj, function (err, result) {
  422. if (err) throw err;
  423. console.log("站点信息插入成功");
  424. db.close();
  425. json['success'] = 'true'
  426. json['data'] = {};
  427. res.send(json);
  428. });
  429. });
  430. */
  431. })
  432. })
  433. // login
  434. // app.get('/login', function (req, res) {
  435. // console.log("/login 响应 请求");
  436. // res.send('login');
  437. // })
  438. var server = app.listen(8081, function () {
  439. var host = server.address().address
  440. var port = server.address().port
  441. console.log("应用实例,访问地址为 http://%s:%s", host, port)
  442. })