-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
127 lines (100 loc) · 2.95 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//constants
const express = require('express');
const app = express();
const session = require('express-session');
const db = require('./database');
app.use(express.urlencoded({ extended: true }));
app.set('view engine', 'pug');
app.use(express.static('public'));
//start session
app.use(session({
secret: 'keyis123',
resave: false,
saveUninitialized: true,
cookie: { secure: 'auto' }
}));
app.use(express.json());
//POST Routes from routes folder
const loginRoutes = require('./routes/loginRoutes');
const registrationRoutes = require('./routes/registrationRoutes');
const dashboardRoutes = require('./routes/dashboardRoutes');
const feedbackRoutes = require('./routes/feedbackRoutes');
const dataRoutes = require('./routes/dataRoutes');
const weatherPredictionRoutes = require('./routes/weatherPredictionRoutes');
app.use('/', loginRoutes);
app.use('/register', registrationRoutes);
app.use('/', dashboardRoutes);
app.use('/', feedbackRoutes);
app.use('/', dataRoutes);
app.use('/', weatherPredictionRoutes)
//Get routes
app.get('/', (req, res) => {
res.render('index', {
title: 'Home Page'
});
});
app.get('/index', (req, res) => {
res.render('index', {
title: 'Home Page'
});
});
app.get('/users', (req, res) => {
res.render('users', {
title: 'Login Page'
});
});
app.get('/users/register', (req, res) => {
res.render('register', {
title: 'Registration Page'
});
});
app.get('/admin', (req, res) => {
res.render('admin', {
title: 'Login Page'
});
});
//special admin logged in route
app.get('/login', (req, res) => {
const data = {};
db.all("SELECT * FROM Feedback", [], (err, feedback) => {
if (err) {
console.error(err);
res.status(500).send('An error occurred retrieving feedback');
return;
}
data.feedback = feedback;
db.all("SELECT * FROM Historical_Data ORDER BY HId DESC", [], (err, historicalData) => {
if (err) {
console.error(err);
res.status(500).send('An error occurred retrieving historical data');
return;
}
data.historicalData = historicalData;
res.render('login', {
...data
});
});
});
});
//special dashboard logged in route
app.get('/dashboard', (req, res) => {
if (req.session.userId) {
res.render('dashboard', {
title: 'User Dashboard',
username: req.session.username,
predictionResult: req.session.predictionResult,
dateForPrediction: req.session.dateForPrediction
});
} else {
res.redirect('/users');
}
});
//middleware
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Internal Server Error');
});
//server
app.listen(1234, () => {
console.log("Server is running at: http://localhost:1234");
});