This repository has been archived by the owner on Apr 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (90 loc) · 2.62 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
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 dependencies
import express from 'express';
import mongoose from 'mongoose';
import dotenv from 'dotenv';
import cors from 'cors';
import { engine } from 'express-handlebars';
import * as dataGetter from './dataGetter.js';
// define ports
const port = 80;
// initialize environment variables
dotenv.config();
// create app
const app = express();
// activate handlebars page renderer
app.engine('handlebars', engine());
app.set('view engine', 'handlebars');
app.set('views', './views');
// import scheduled jobs
import scheduler from './scheduler.js';
import scheduledJobs from './handlers/jobs.js';
// activate sheduled jobs for data gathering if process argument is set (see package.json)
if (process.argv.slice(2) == 'datagather') {
scheduler.initCrons(scheduledJobs);
}
// connect to db
mongoose.connect(process.env.MONGO_CONNECTION, {
useNewUrlParser: true,
});
// mongoose.set('debug', true)
const db = mongoose.connection;
db.on('error', (err) => console.log(err));
db.once('open', () => console.log('Connected to Database'));
// app uses
app.use(express.json({ limit: '50mb' }));
app.use(
express.urlencoded({ limit: '50mb', extended: true, parameterLimit: 50000 })
);
app.use(cors());
// Views
app.get('/', async (req, res) => {
res.render('home', {
records: await dataGetter.getRecordCount(),
approxDataPoints: (await dataGetter.getRecordCount()) * 2,
pageTitle: 'Home',
});
});
app.get('/apple', async (req, res) => {
res.render('analysis', {
pageTitle: 'Apple',
data: JSON.stringify(await dataGetter.destructureDocuments('apple')),
});
});
app.get('/meta', async (req, res) => {
res.render('analysis', {
pageTitle: 'Meta',
data: JSON.stringify(
await dataGetter.destructureDocuments('meta_platforms')
),
});
});
app.get('/microsoft', async (req, res) => {
res.render('analysis', {
pageTitle: 'Microsoft',
data: JSON.stringify(await dataGetter.destructureDocuments('microsoft')),
});
});
app.get('/alphabet', async (req, res) => {
res.render('analysis', {
pageTitle: 'Alphabet',
data: JSON.stringify(await dataGetter.destructureDocuments('alphabet')),
});
});
app.get('/amazon', async (req, res) => {
res.render('analysis', {
pageTitle: 'Amazon',
data: JSON.stringify(await dataGetter.destructureDocuments('amazon')),
});
});
app.get('/tesla', async (req, res) => {
res.render('analysis', {
pageTitle: 'Tesla',
data: JSON.stringify(await dataGetter.destructureDocuments('tesla')),
});
});
// serve static ressources
app.use(express.static('public'));
// Listen
app.listen(port, () => {
console.log(`Listening at http://localhost:${port}`);
});