-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
186 lines (147 loc) · 3.74 KB
/
server.js
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
const express = require("express");
const path = require("path");
const bodyParser =require("body-parser");
const port = 3000;
const app = express();
// -----------------------------------------------------
// -----------------------------------------------------
//mysql启动
const Sequelize = require("sequelize");
const sequelize = new Sequelize(
"mysql://root:[email protected]:3306/study",
{
pool:{
max:50,
min:10,
acquire:30000,
idle:10000
},
define:{
timestamp:false
},
dialect:"mysql"
}
);
const user = sequelize.define("user",{
name:{
type:Sequelize.STRING,
allowNull:true,
},psw:{
type:Sequelize.STRING,
allowNull:true,
},email:{
type:Sequelize.STRING,
allowNull:true,
},enable:{
type:Sequelize.STRING,
allowNull:false,
}
},
{
freezeTableName:true,
},
);
const record = sequelize.define("record",{
uid:{
type:Sequelize.INTEGER,
allowNull:true,
},videos:{
type:Sequelize.STRING,
allowNull:true,
},times:{
type:Sequelize.INTEGER,
allowNull:true,
},qcode:{
type:Sequelize.STRING,
allowNull:true,
}
},
{
freezeTableName:true,
},
);
sequelize
.authenticate()
.then(()=>{
console.log(">数据库连接成功!!");
app.listen(port,()=>{
console.log(">运行端口为" +port+"!!");
})
})
.catch(err =>{
console.error(">连接失败!!:",err)
});
//body parser middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended:false }));
app.use(express.static(path.join(__dirname,"public")));
console.log(__dirname);
//session begin
var session = require('express-session');
var sess = {
saveUninitialized: true,
resave: false,
secret: 'keyboard jianghai ',
// cookie: { maxAge: 60000 }
};
if (app.get('env') === 'production') {
app.set('trust proxy', 1); // trust first proxy
sess.cookie.secure = true // serve secure cookies
}
app.use(session(sess));
// session end
//view setup
app.set("views",path.join(__dirname,"views"));
app.set("view engine","ejs");
//index
app.get("/",(req,res,next)=>{
//1.首页限制
if (!req.session.name) {
console.log("未登录");
res.redirect('/login');
return
}
res.render("index",{
msg:{ code:"good",txt:req.session.name }
});
});
app.get("/login",(req,res,next)=>{
res.render("login",{
msg:{}
});
});
app.post("/login",(req,res,next)=>{
_name = req.body.name;
_psw = req.body.psw;
console.log("名字为 %s 密码为 %s", _name,_psw);
user.findOne({ where:{name:_name,psw:_psw,enable:1} }).then(rows=>{
console.log(">查询结果:",JSON.stringify(rows,null,4));
if (rows){
req.session.name = _name;
req.session.enable = 1;
res.redirect("/");
} else {
res.render("login",{
msg:{ code:"2333~~",txt:"~~~用户名或者密码错误啦~~" }
});
}
})
});
app.get("/videos/:num",(req,res,next)=>{
res.render("videos")
});
app.get("/statisticalData",(req,res,next)=>{
res.render("statisticalData")
});
app.get("/dataTables",(req,res,next)=>{
res.render("dataTables")
});
app.get("/danmu",(req,res,next)=>{
res.render("danmu")
});
app.get("/QuestionBank",(req,res,next)=>{
res.render("QuestionBank")
});
app.get("/QuestionBankBatchImport",(req,res,next)=>{
res.render("QuestionBankBatchImport")
});