-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbank-03.js
55 lines (46 loc) · 1.3 KB
/
bank-03.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
const express = require('express')
const { createReadStream } = require('fs')
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const { randomBytes } = require('crypto')
const app = express()
app.use(bodyParser.urlencoded({extended: false}))
app.use(cookieParser())
const USERS = {
alice: '123',
bob: 'eagle'
}
const BALANCES = {
alice: 500,
bob: 100
}
const SESSIONS = {} // sessionId -> username
app.get('/', (req, res) => {
const sessionId = req.cookies.sessionId
const username = SESSIONS[sessionId]
if (username) {
// note: use backquotes `` for ${var} trick
res.send(`Hi ${username}! Your balance is $${BALANCES[username]} <p>You can <a href=/logout>logout</a>`)
} else {
createReadStream('index-01.html').pipe(res)
}
})
app.post('/login', (req, res) => {
const username = req.body.username
const password = USERS[username]
if (req.body.password === password) {
const sessionId = randomBytes(16).toString('hex')
SESSIONS[sessionId] = username
res.cookie('sessionId',sessionId)
res.redirect('/')
} else {
res.send('fail!')
}
})
app.get('/logout', (req,res) => {
const sessionId = req.cookies.sessionId
delete SESSIONS[sessionId]
res.clearCookie('sessionId')
res.redirect('/')
})
app.listen(4000)