-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
114 lines (87 loc) · 2.58 KB
/
app.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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const session = require('express-session');
function Word() {
this.initialize.apply(this, arguments);
}
Word.prototype.initialize = function(word, trans, sent, prio, impo) {
this.word = word;
this.trans = trans;
this.sent = sent; // sentence
this.prio = prio; // priority
this.impo = impo; // importance
}
function check_login_info(username, password) {
if(username.length <= 2) return false;
if(username.length >= 20) return false;
if(password.length <= 5) return false;
if(password.length >= 20) return false;
return true;
}
app.use(bodyParser.json());
app.use(cookieParser());
app.use(session({
secret: "12345",
cookie: {maxAge: 80000},
}))
// 重定向到主页
app.get("/", (req, res) => {
console.log("redirected to main");
res.redirect("/webs/main/index.html");
})
app.post("/register", (req, res) => {
console.log(req.body);
var temp = req.body;
var username = temp.username;
var password = temp.password;
if(!check_login_info(username, password)) {
res.send("failed: username / password is invalid"); return;
}
// db operation
req.session.userid = 1;
res.send("ok");
})
//登陆
app.post("/login", (req, res) => {
console.log(req.body);
var temp = req.body;
var username = temp.username;
var password = temp.password;
if(!check_login_info(username, password)) {
res.send("failed: username / password is invalid"); return;
}
// interact with database;
req.session.userid = 1; // for test;
res.send("ok");
})
// 添加词汇
app.post("/addVoca", (req, res) => {
console.log(req.body);
// interact with database;
res.send("ok");
})
// 获取词汇列表
app.get("/getVocaList", (req, res) => {
console.log(req.session);
// interact with database;
// these codes are just for test
var content = [];
content.push(new Word("word1", "trans1", "sent1", 1, 2));
content.push(new Word("c", "d", "f", 3, 4));
console.log(content);
res.send(JSON.stringify(content));
})
// 获取前端自资源
app.use("/webs", (req, res, next) => {
console.log("send file (webs)");
res.sendFile(__dirname + req.originalUrl);
return;
})
app.use("/lib", (req, res, next) => {
console.log("send file (lib)");
res.sendFile(__dirname + req.originalUrl);
return;
})
app.listen(8080, () => console.log("listening on port 8080"));