-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
102 lines (91 loc) · 2.2 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
import express from 'express';
import session from 'express-session';
import lodash from 'lodash';
import morgan from 'morgan';
import nunjucks from 'nunjucks';
import ViteExpress from 'vite-express';
const app = express();
const port = '8000';
app.use(morgan('dev'));
app.use(express.urlencoded({ extended: false }));
app.use(express.static('public'));
app.use(session({ secret: 'ssshhhhh', saveUninitialized: true, resave: false }));
nunjucks.configure('views', {
autoescape: true,
express: app,
});
const MOST_LIKED_FOSSILS = {
aust: {
img: '/img/australopith.png',
name: 'Australopithecus',
num_likes: 584,
},
quetz: {
img: '/img/quetzal_torso.png',
name: 'Quetzal',
num_likes: 587,
},
steg: {
img: '/img/stego_skull.png',
name: 'Stegosaurus',
num_likes: 598,
},
trex: {
img: '/img/trex_skull.png',
name: 'Tyrannosaurus Rex',
num_likes: 601,
},
};
const OTHER_FOSSILS = [
{
img: '/img/ammonite.png',
name: 'Ammonite',
},
{
img: '/img/mammoth_skull.png',
name: 'Mammoth',
},
{
img: '/img/ophthalmo_skull.png',
name: 'Opthalmosaurus',
},
{
img: '/img/tricera_skull.png',
name: 'Triceratops',
},
];
app.get('/top-fossils', (req, res) => {
if (req.session.name) {
const fossils = Object.values(MOST_LIKED_FOSSILS)
res.render('top-fossils.html.njk', { fossils: fossils, name: req.session.name });
} else {
res.redirect('/')
};
});
app.get('/', (req, res) => {
if (req.session.name) {
res.redirect('/top-fossils')
} else {
res.render('homepage.html.njk')
}
});
app.get('/get-name', (req, res) => {
req.session.name = req.query.name;
res.redirect('/top-fossils')
});
app.post('/like-fossil', (req, res) => {
const fossilChoice = req.body['fossil-choice'];
if (fossilChoice) {
MOST_LIKED_FOSSILS[fossilChoice].num_likes += 1;
res.render('thank-you.html.njk', {name: req.session.name});
} else {
res.send('Please make a selection');
};
});
app.get('/random-fossil.json', (req, res) => {
const randomFossil = lodash.sample(OTHER_FOSSILS);
res.json(randomFossil);
});
ViteExpress.listen(app, port, () => {
console.log(`Server running on http://localhost:${port}...`);
});