1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
| const fs = require('fs') const express = require('express') const bodyParser = require('body-parser') const lodash = require('lodash') const session = require('express-session') const randomize = require('randomatic') const mysql = require('mysql') const mysqlConfig = require("./config/mysql") const ejs = require('ejs')
const pool = mysql.createPool(mysqlConfig)
const app = express()
app.use(bodyParser.urlencoded({extended: true})).use(bodyParser.json()) app.use('/static', express.static('static')) app.use(session({ name: 'session', secret: randomize('aA0', 16), resave: false, saveUninitialized: false }))
function setHeader(req,res,next){ res.set({ "X-DNS-Prefetch-Control":"off", "X-Frame-Options": "SAMEORIGIN", "X-Download-Options": "noopen", "X-Content-Type-Options": "nosniff" }); next(); }
app.use(setHeader);
app.set('json escape',true) app.set('views', './views') app.set('view engine', 'ejs')
function auth(req,res,next){ if(!req.session.login || !req.session.userid ){ res.redirect(302,"/login"); } else{ next(); } }
let query = function( sql, values ) { return new Promise(( resolve, reject ) => { pool.getConnection(function(err, connection) { if (err) { reject( err ) } else { connection.query(sql, values, ( err, rows) => { if ( err ) { reject( err ) } else { resolve( rows ) } connection.release() }) } }) }) }
app.get("/",auth,function(req,res,next){ res.render('index'); })
app.get("/sandbox",auth,function(req,res,next){ res.render("sandbox"); })
app.all('/login',async function(req,res,next){
if( req.method == 'POST' ){ if(req.body.username && req.body.password){ var username = req.body.username; var password = req.body.password;
var sql = "select id from `user` where username=? and password=?";
let dataList = await query( sql,[username,password])
if(dataList.length == 0){ res.send("<script> alert('用户名或密码错误.'); location.replace('/login'); </script>"); return ; } console.log(dataList); req.session.userid = dataList[0].id ; req.session.login = true ; res.redirect(302,"/"); return ;
} }
res.render('login_register',{ title:" storeHtml | logins ", buttonHintF:"登 录", buttonHintS:"没有账号?", hint:"登录", next:"/register" }); });
app.all('/register',async function(req,res,next){
console.log(req.body);
if( req.method == 'POST' ){
if(req.body.username && req.body.password){
var username = req.body.username; var password = req.body.password; var sql = "select id from `user` where username=?";
let dataList = await query( sql,[username])
if(dataList.length>0){ res.send("<script>alert('用户名重复'); location.replace('/register');</script>"); return ; } var sql = "insert into `user` (`username`,`password`) values (?,?) "; let result = await query( sql,[username,password]); console.log(result);
if(result.affectedRows>0){ res.send("<script>alert('注册成功');location.replace('/login');</script>") return; }else{ res.send("<script>alert('注册失败');</script>"); return; } } }
res.render('login_register',{ title:" storeHtml | register ", buttonHintF:"注 册", buttonHintS:"已有账号?", hint:"注册", next:"/login" }); });
app.get("/get",auth,async function(req,res,next){
var userid = req.session.userid ; var sql = "select count(*) count from `html` where userid= ?" var dataList = await query(sql,[userid]);
if(dataList[0].count == 0 ){ res.json({})
}else if(dataList[0].count > 5) { console.log("Merge the recorder in the database.");
var sql = "select `id`,`dom` from `html` where userid=? "; var raws = await query(sql,[userid]); var doms = {} var ret = new Array();
for(var i=0;i<raws.length ;i++){ lodash.defaultsDeep(doms,JSON.parse( raws[i].dom ));
var sql = "delete from `html` where id = ?"; var result = await query(sql,raws[i].id); } var sql = "insert into `html` (`userid`,`dom`) values (?,?) "; var result = await query(sql,[userid, JSON.stringify(doms) ]);
if(result.affectedRows > 0){ ret.push(doms); res.json(ret); }else{ res.json([{}]); }
}else {
console.log("Return recorder is less than 5,so return it without merge."); var sql = "select `dom` from `html` where userid=? "; var raws = await query(sql,[userid]); var ret = new Array();
for( var i =0 ;i< raws.length ; i++){ ret.push(JSON.parse( raws[i].dom )); }
console.log(ret); res.json(ret); }
});
app.post("/add",auth,async function(req,res,next){
if(req.body.type && req.body.content){
var newContent = {} var userid = req.session.userid;
newContent[req.body.type] = [ req.body.content ]
console.log("newContent:",newContent);
var sql = "insert into `html` (`userid`,`dom`) values (?,?) "; var result = await query(sql,[userid, JSON.stringify(newContent) ]);
if(result.affectedRows > 0){ res.json(newContent); }else{ res.json({}); }
}
});
var server = app.listen(80, function() { console.log('Listening on port %d', server.address().port); });
|