-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
299 lines (222 loc) · 6.52 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
if (process.env.NODE_ENV !== "production") {
require("dotenv").config()
}
// Importing Libraies that we installed using npm
const express = require("express")
//var MongoClient = require('mongodb').MongoClient;
const assert = require("assert")
//const bodyParser = require("body-parser")
const ejs = require("ejs")
const _ = require("lodash")
const app = express()
const bcrypt = require("bcrypt") // Importing bcrypt package
const passport = require("passport")
const initializePassport = require("./passport-config")
const flash = require("express-flash")
const session = require("express-session")
const methodOverride = require("method-override")
const dbConnect = require('./mongodb');
const dbConnect2 = require("./mongodb2")
const dbConnect3 = require("./mongodb3")
//const dbConnect2 = require('./mongodb2'); ***no need as type will separate students and teachers***
//var t;
//***function to insert users list in database***
const insertMany = async ()=>{
const db = await dbConnect();
const result = await db.insertMany(users)
if(result.acknowledged){
console.log("data inserted")
}
//const db = await dbConnect();
//const result = await db.insertMany(users)
/*if(result.acknowledged){
console.log("data inserted")
}*/
}
//**********database for student connected*************
const insert = async ()=>{
const db = await dbConnect2();
const result = await db.insertMany(stu)
if(result.acknowledged){
console.log("data inserted")
}
//const db = await dbConnect();
//const result = await db.insertMany(users)
/*if(result.acknowledged){
console.log("data inserted")
}*/
}
//***********databse for teacher connected*************
const insert2 = async ()=>{
const db = await dbConnect3();
const result = await db.insertMany(tea)
if(result.acknowledged){
console.log("data inserted")
}
}
initializePassport(
passport,
email => users.find(user => user.email === email),
id => users.find(user => user.id === id)
)
const users = []
app.use( express.static( "views" ) );
app.use(express.urlencoded({extended: false}))
app.use(flash())
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false, // wont resave the session variable if nothing is changed
saveUninitialized: false
}))
app.use(passport.initialize())
app.use(passport.session())
app.use(methodOverride("_method"))
// Configuring the register post functionality
app.post("/login", checkNotAuthenticated, passport.authenticate("local", {
successRedirect: "/",
failureRedirect: "/login",
failureFlash: true
}))
// Configuring the register post functionality
app.post("/register", checkNotAuthenticated, async (req, res) => {
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10)
users.push({
id: Date.now().toString(),
name: req.body.name,
email: req.body.email,
password: hashedPassword,
type: req.body.sub,
})
//t = req.body.radio;
insertMany()
//bulk.append(users.copy())
//***********here database is to be connected************
console.log(users); // Display newly registered in the console
// insert();
res.redirect("/login")
} catch (e) {
console.log(e);
res.redirect("/register")
}
})
//configuring the student post functionality
const stu = []
app.post("/student", async (req, res) => {
try {
stu.push({
id: Date.now().toString(),
name: req.body.name,
class: req.body.car,
section: req.body.cars,
})
insert()
//***********here database is to be connected************
console.log(stu); // Display newly registered in the console
res.redirect("/save")
} catch (e) {
console.log(e);
}
})
//configuring the teacher post functionality
const tea = []
app.post("/teacher", async (req, res) => {
try {
tea.push({
id: Date.now().toString(),
name: req.body.name,
class: req.body.amber,
section: req.body.skar,
})
insert2()
console.log(tea);
res.redirect("/save")
}
catch (e) {
console.log(e);
}
})
app.set('view engine', 'ejs')
//var url = 'mongodb://localhost:27017/headmasterstudents';
let MongoClient = require('mongodb').MongoClient;
var db;
var db2;
MongoClient.connect('mongodb://localhost:27017', function (err, client) {
if (err) throw err;
db = client.db('headmasterstudents');
//app.listen(3000);
});
MongoClient.connect('mongodb://localhost:27017', function (err, client) {
if (err) throw err;
db2 = client.db('headmasterteacher');
//app.listen(3000);
});
// Routes
app.get('/', checkAuthenticated, (req, res) => {
res.render("index.ejs", {name: req.user.name})
})
app.get('/student', (req, res) => {
db.collection('data2')
.find()
.toArray(function (err, result) {
if (err) throw err;
res.render('studentSchedule', {result: result});
});
})
app.get('/teacher', (req, res) => {
db2.collection('data3')
.find()
.toArray(function (err, DATA) {
if (err) throw err;
res.render('teacherSchedule', {DATA: DATA});
});
})
app.get('/login', checkNotAuthenticated, (req, res) => {
res.render("login.ejs")
})
app.get('/register', checkNotAuthenticated, (req, res) => {
res.render("register.ejs")
})
app.get('/headmaster', (req, res) => {
res.render('headmaster.ejs')
})
app.get('/index_head', (req, res) => {
res.render('index_head.ejs')
})
app.get('/students', (req, res) => {
res.render('student.ejs')
})
app.get('/teachers', (req, res) => {
res.render('teacher.ejs')
})
app.get('/save', (req, res) => {
res.render('save.ejs')
})
app.get('/home', (req, res) => {
res.render('home.ejs')
})
// End Routes
// app.delete('/logout', (req, res) => {
// req.logOut()
// res.redirect('/login')
// })
app.delete("/logout", (req, res) => {
req.logout(req.user, err => {
if (err) return next(err)
res.redirect("/")
})
//bulk.append(users.copy())
})
function checkAuthenticated(req, res, next){
if(req.isAuthenticated()){
return next()
}
res.redirect("/login")
}
function checkNotAuthenticated(req, res, next){
if(req.isAuthenticated()){
return res.redirect("/")
}
next()
}
app.listen(3000)