-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_session_file.js
78 lines (75 loc) · 1.97 KB
/
app_session_file.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
var express = require('express');
var session = require('express-session');
var FileStore = require('session-file-store')(session);
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({extended: false}));
app.use(session({
secret: '1234564546@$skjdljasaaads',
resave: false, //세션 id를 새로 만드는 것.
saveUninitialized: true, //세션 id를 세션을 실행하기 전까지 발급하지 않음
store: new FileStore()
}));
app.get('/count',function(req,res){
if(req.session.count){
req.session.count++;
} else{
req.session.count = 1;
}
res.send('count : ' + req.session.count);
});
app.get('/tmp',function(req,res){
res.send('result : ' + req.session.count);
});
app.get('/auth/logout',function(req,res){
req.session.destroy(); //세션 삭제
res.redirect('/welcome');
});
app.get('/welcome',function(req,res){
if(req.session.displayName){
res.send(`
<h1>Hello, ${req.session.displayName}</h1>
<a href="/auth/logout">logout</a>
`);
} else{
res.send(`
<h1>Welcome</h1>
<a href="/auth/login">Login</a>
`)
}
});
app.post('/auth/login', function(req,res){
var user = {
username:'egoing',
password:'111',
displayName:'Egoing'
};
var uname = req.body.username;
var pwd = req.body.password;
if(uname === user.username && pwd === user.password){
req.session.displayName = user.displayName;
res.redirect('/welcome');
} else{
res.send('Who are you? <a href="/auth/login">login</a>');
}
});
app.get('/auth/login', function(req,res){
var output = `
<h1>Login</h1>
<form action = "/auth/login" method = "post">
<p>
<input type = "text" name = "username" placeholder = "username">
</p>
<p>
<input type = "password" name="password" placeholder = "password">
</p>
<p>
<input type ="submit">
</p>
</form>
`;
res.send(output);
});
app.listen(3003,function(){
console.log('Connected 3003 port!!!');
});