-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
210 lines (147 loc) · 4.92 KB
/
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
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
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var urlEncodedParser = bodyParser.urlencoded({extended: false});
var path = require("path");
var mysql = require("mysql");
// connect strings for mysql
// var connection = mysql.createConnection({
// host: "127.0.0.1",
// user: "root",
// password: "root",
// database: "dbms"
// });
// var session = require('express-session');
// var MySQLStore = require('express-mysql-session')(session);
// var options = {
// host: '127.0.0.1',
// port: 3306,
// user: 'root',
// password: 'root',
// database: 'dbms'
// };
// var sessionStore = new MySQLStore(options);
//
// app.use(session({
// key: 'session_cookie_name',
// secret: 'session_cookie_secret',
// store: sessionStore,
// resave: false,
// saveUninitialized: false
// }));
// // connecting ......
// connection.connect();
// app.use(session({secret: "secret"}));
app.use(bodyParser.text());
app.use(express.static('templates'));
var connection = 0;
//app.set('views', __dirname + '/views')
//app.set('view engine', 'ejs');
//app.engine('html', require('ejs').renderFile);
//app.set('view engine', 'html');
var exphbs = require('express-handlebars');
var hbs = exphbs.create({ /* config */ });
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
//app.engine('html', require('hbs').__express);
// app.engine('html', require('ejs').renderFile);
// app.set('view engine', 'html');
//app.set('view engine', 'pug');
//app.set('views','./views');
app.get('/', function(req, res) {
// if (req.session.username) {
// res.redirect('/dashboard');
// }
res.sendFile('home.html', { root: path.join(__dirname, 'templates')});
});
/*app.get('/login', function(req, res){
res.sendFile('login.html', { root: path.join(__dirname, 'templates') });
});
*/
app.post('/login', urlEncodedParser, function(request, response){
//res.sendFile('success.html', { root: path.join(__dirname, 'templates') });
console.log(request.body.username + " : USN\n\n" + request.body.password + " : password\n\n");
var login = require('./login.js');
login.Login(connection, request, response);
});
app.get('/register', function(req, res){
res.sendFile('register.html', { root: path.join(__dirname, 'templates') });
});
app.get('/dashboard', function(req, res) {
if (req.session.username) {
//res.render('dashboard', { name: req.session.FirstName, USN: req.session.USN });
var dashboard = require("./dashboard.js");
dashboard.Dashboard(connection, req, res);
}
else {
res.redirect('/');
}
});
app.post('/register', urlEncodedParser, function(req, res){
var body = req.body;
var reg = require('./register.js');
reg.Reg(connection, body, res);
});
app.get('/password_change', urlEncodedParser, function (req, res){
res.render('password_change');
});
app.post('/password_change', urlEncodedParser, function (req, res){
//var body = req.body;
console.log("OITSIDE" + req.session.username);
var password_change = require ("./password_change.js");
password_change.PasswordChange(req, res, connection);
});
app.get('/add_company_test', function(req, res){
var retrieveCompanies;
connection.query("SELECT * FROM COMPANY", function(err, result){
console.log("\n\nCOM LIST: " + result[0].Name + "\n\n");
var companyNames = [];
for(var i=0, l=result.length; i<l; i++) {
companyNames.push(result[i].Name);
}
console.log(companyNames);
//var comList = {companies: companyNames};
res.render('addCompanyTest', {Companies: companyNames});
});
});
app.post('/add_company_test', urlEncodedParser, function(req, res){
var addCompanyTest = require('./addCompanyTest.js');
var body = req.body;
var companyName = body.companyname;
console.log("FORM CNAME = " + companyName);
connection.query("SELECT * FROM COMPANY WHERE NAME LIKE '" + companyName + "%';", function(error, result){
if (error) throw error;
else
{
console.log(result);
var companyID = result[0].CompanyID;
addCompanyTest.addTest(req, res, connection, companyID);
}
});
});
app.post('/testregister', urlEncodedParser, function(req, res){
console.log("ID" + req.body.ID);
var body = req.body;
var insertVals =
{
USN: req.session.username,
TestID: body.ID
}
connection.query("INSERT INTO REGISTER SET ?", insertVals, function(error, result){
if(error)
{
console.log("ERROR");
throw error;
}
else
{
console.log("TESTREG SUCCESS");
res.send({status: 200});
}
});
});
app.get('/logout', function(req, res){
req.session.destroy();
res.redirect('/');
});
app.listen(80);