-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
40 lines (30 loc) · 868 Bytes
/
server.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
require('dotenv').config();
const fs = require('fs');
const express = require('express');
const app = express();
app.set('view engine', 'pug');
app.use(express.json());
app.use(express.static('public'));
app.get('/', (req, res) => {
const media = getMedia();
res.render('index', { media });
});
app.get('/random', (req, res) => {
const media = getMedia();
const randomMedia = media[Math.floor(Math.random() * media.length)];
res.redirect(`/v/${randomMedia.id}`);
});
app.get('/v/:id', (req, res) => {
const { id } = req.params;
const media = getMedia().find(m => m.id == id);
res.render('view', { media });
});
app.use((req, res, next) => {
res.status(404).render('404');
});
app.listen(process.env.PORT, () => {
console.log(`Server running on port ${process.env.PORT}`);
});
function getMedia() {
return JSON.parse(fs.readFileSync('db.json'));
}