-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (58 loc) · 2.17 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
const port = 3300;
const express = require('express');
const app = express();
var bodyParser = require('body-parser');
const fs = require('fs');
const cookieParser = require('cookie-parser');
require('dotenv').config()
app.use(cookieParser())
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// eigen functies
const cookie = require('./server/disclaimer.js');
const database = require('./server/database.js');
app.use(express.static(__dirname +'/public'));
app.set('view engine', 'ejs');
app.get('/data', function(req, res) {
const data = require('./public/data/data.json');
res.send(data);
});
const quizRouter = require('./routes/quiz.js');
app.use('/quiz', quizRouter);
const adminRouter = require('./routes/admin.js');
app.use('/dashboard', adminRouter);
const apiRouter = require('./routes/api.js');
const { query } = require('express');
app.use('/api', apiRouter);
// dynamic route
// deze bullshit opruimen
app.get('/:id', (req, res) => {
// check if filename exists
if(req.params.id == "" || req.params.id == null || req.params.id == undefined || req.params.id == "/"){
res.render(__dirname + '/views/index', {disclaimer: cookie.checkCookies(req.cookies)});;
}
fs.stat(__dirname + '/views/' + req.params.id + ".ejs", function(err, stat) {
if (err == null) {
res.render(__dirname + '/views/' + req.params.id, {disclaimer: cookie.checkCookies(req.cookies)});
} else {
console.log(err);
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
database.badPages(req.url, ip);
res.status(404).sendFile(__dirname + '/views/404.html');
}
});
});
// homepage
app.get('/', (req, res) => {
res.render(__dirname + '/views/index', {disclaimer: cookie.checkCookies(req.cookies)});
});
// 404 handler
app.use(function (req,res){
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
database.badPages(req.url, ip);
res.status(404).sendFile(__dirname + '/views/404.html');
});
// server luisterd naar de port
app.listen(port, () => {
console.log(`Server is running op http://localhost:${port}`);
});