-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
78 lines (68 loc) · 2.24 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
const {userDB} = require("./data/data");
const path = require("path");
const bodyParser = require("body-parser");
const http = require("http");
const express = require("express");
var session = require('express-session');
const uuid4 = require('uuid4')
const app = express();
const server = http.createServer(app);
const PORT = 3000;
app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname,"public")));
app.use(session({
secret: uuid4(),
cookie: { maxAge: 600 * 1000 }
}));
app.set("views", path.join(__dirname,"public", "views"));
app.set("view engine", "pug");
app.get('/',(req,res)=>{
res.render('index');
req.session.id ,req.session.password ,req.session.username = null ,null ,null;
})
app.get('/login',(req,res)=>{
res.render('login');
})
app.get('/signup',(req,res)=>{
res.render('signup');
})
app.post('/signup',(req,res)=>{
try{
const findUser = userDB.find(data => data.id === req.body.id);
if(!findUser){
const newUser = {
username: req.body.username,
id: req.body.id,
password: req.body.password,
}
userDB.push(newUser);
req.session.id ,req.session.password ,req.session.username = newUser.id ,newUser.password , newUser.username;
res.redirect(`/user/${req.session.username}`);
}else{
res.redirect(`/login`);
}
}catch(err){
res.send("伺服器錯誤:(" + err);
}
})
app.post('/login',(req,res)=>{
try{
const findUser = userDB.find(data => data.id === req.body.id);
if(findUser && req.body.password === findUser.password){
req.session.id ,req.session.password ,req.session.username= findUser.id ,findUser.password , findUser.username;
res.redirect(`/user/${req.session.username}`);
}else{
res.render('login',{
message:"查無此帳"
})
}
}catch{
res.send("伺服器錯誤:("+err);
}
})
app.get('/user/:username',(req,res)=>{
res.render('links');
})
server.listen(PORT,()=>{
console.log(`server is listening on http://localhost:${PORT}`);
})