-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
42 lines (31 loc) · 963 Bytes
/
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
const express = require('express');
const app = express();
const port = 3001;
const session = require('express-session');
let names = ['Mehmet', 'Tuncay', 'Burçin', 'Tuğçe', 'Gülali'];
app.use(express.static('public'));
app.use(session({
secret: 'raffle_secret',
resave: false,
saveUninitialized: true
}));
app.get('/raffle', (req, res) => {
if (req.session.nameDrawn) {
res.send(`You have already drawn a name: ${req.session.nameDrawn}`);
return;
}
if (names.length === 0) {
res.send("All names have been drawn.");
return;
}
const index = Math.floor(Math.random() * names.length);
const name = names.splice(index, 1)[0];
req.session.nameDrawn = name;
res.send(`Congratulations, ${name}!`);
});
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.listen(port, () => {
console.log(`Raffle app listening at http://localhost:${port}`);
});