-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (34 loc) · 1.1 KB
/
index.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
const express = require('express');
const path = require('path');
const redditData = require('./data.json');
const app = express();
app.use(express.static(path.join(__dirname, '/public')));
// linking css, js, and other assets from the public folder
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, '/views')); // enables to use nodemon outside the current directory
app.get('/', (req, res) => {
res.render('home')
})
app.get('/r/:subreddit', (req, res) => {
const {subreddit} = req.params;
const data = redditData[subreddit];
if(data) {
res.render('subreddit', {...data})
} else {
res.render('notfound', {subreddit})
}
})
app.get('/rand', (req, res) => {
const num = Math.floor(Math.random() * 10) + 1
// key-value pairs that are accessible in the ejs html template
res.render('random', {num, title: 'random'});
})
app.get('/cats', (req, res) => {
const cats = [
'Blue', 'Rocket', 'Monty', 'Stephanie', 'Winston'
]
res.render('cats', {cats})
})
app.listen(3000, () => {
console.log('Listening on port 3000')
})