-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
executable file
·251 lines (185 loc) · 5.56 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var mysql = require('mysql');
require('dotenv').config();
var cookieParser = require('cookie-parser');
var expressValidator = require('express-validator');
var flash = require('connect-flash');
var session = require('express-session');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var mongo = require('mongodb');
var mongoose = require('mongoose');
//Connecting to mongoose
mongoose.connect('mongodb://localhost/cs_trackulum_users');
var db = mongoose.connection;
//Routes
var routes = require('./routes/index');
var users = require('./routes/users');
//Init App
var app = express();
//Set static Path
app.use(express.static(path.join(__dirname,'public')));
app.use('/users', express.static('public'));
//View Engine
app.set('view engine' , 'ejs');
app.set('views', path.join(__dirname,'views'));
//Middleware
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.use(cookieParser());
// Express Session
app.use(session({
secret: 'secret',
saveUninitialized: true,
resave: true
}));
// Passport init
app.use(passport.initialize());
app.use(passport.session());
// Express Validator
app.use(expressValidator({
errorFormatter: function(param, msg, value) {
var namespace = param.split('.')
, root = namespace.shift()
, formParam = root;
while(namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
param : formParam,
msg : msg,
value : value
};
}
}));
// Connect Flash
app.use(flash());
// Global Vars
app.use(function (req, res, next) {
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash('error');
res.locals.user = req.user || null;
next();
});
app.use('/',routes);
app.use('/users',users);
//Creating connection with SQL database
var connection = mysql.createConnection({
host : 'sql12.freesqldatabase.com',
user : 'sql12280273',
password : 'sSiHHrnh6D',
database : 'sql12280273'
});
//Starting App
app.listen(3000,function(){
console.log("Server started on port 3000");
});
//Render homepage
/*app.get('/',function(req,res){
res.render('homepage');
});
*/
app.get('/authentication_page',function(req,res){
res.render('authentication_page');
});
//Guest-Page
app.get('/guest_page',function(req,res){
connection.query('SELECT * FROM Universities', function (err, result, fields) {
if(err)
throw err;
else
res.render('uni_list',{dropdownvals: result});
});
});
//Render courses based on university id
app.get('/courses', function(req,res){
var unicourseid = req.query.id;
var uniname;
connection.query('SELECT uname FROM Universities WHERE uid='+ unicourseid, function(err,result,fields){
if(err)
throw err;
else
uniname = result[0].uname;
});
connection.query('SELECT * FROM courses WHERE uni_course_id =' + unicourseid, function(err,result,fields){
if(err)
throw err;
else
res.render('courses',{courselist: result, uniname: uniname });
});
});
//Handle Module requests
app.get('/modules',function(req,res){
var courseid = req.query.id;
console.log(courseid);
var modulequery = "SELECT * FROM modules,courses_modules WHERE courses_modules.course_id =" + courseid + " AND courses_modules.module_id = modules.moduleid";
connection.query(modulequery,function(err,result,fields){
if(err)
throw err
else
res.render('modules', {modulelist: result});
});
});
//Handle Individual Module Information
app.get('/module_display',function(req,res){
var module_code = req.query.code;
connection.query('SELECT * FROM modules WHERE modulecode="' + module_code + '"' , function(err,result,fields){
if(err)
throw err
else
res.render('modules', {modulelist:result});
});
});
//Handle academic user page
app.get('/academic_user_page',ensureAuthenticated,function(req,res){
res.render('academic_user_page');
});
//SQL DB Handling
app.get('/sql_db',function(req,res){
connection.query('SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA="sql12280273" ', function(err,results,fields){
if(err)
{
throw err;
}
else
{
res.render('sql_db', {table_names :results});
}
});
});
// Handling the request for SQL queries
app.get('/sql_db_dat',function(req,res){
var sqlquery = req.query.queryname;
console.log("passed");
console.log(sqlquery);
connection.query(sqlquery,function(err,results,fields){
if(err)
{
var status = {status: "failed"};
data = {status,err};
console.log("error sent");
res.send(data);
}
else{
console.log("sent");
var status = {status : "passed"};
data = {status,fields,results}
res.send(data);
}
});
});
//Displaying welcome page for PopulateDB
app.get('/populatedb_welcome',function(req,res){
res.render('populatedb_welcome');
});
function ensureAuthenticated(req, res, next){
if(req.isAuthenticated()){
return next();
} else {
//req.flash('error_msg','You are not logged in');
res.redirect('/users/login');
}
}