-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
588 lines (568 loc) · 15.5 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
// Requiring All Modules
require("dotenv").config()
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const path = require("path");
const port = 3000;
const bycrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const connectDatabase = require("./connection/database.connect")
connectDatabase(process.env.MOGODB_URI)
// Databases Requires
const { SignUpUserModel } = require("./model/user");
const { AdmissionUserModel } = require("./model/admission");
const { DescModel } = require("./model/description");
const { TeachersUserModel } = require("./model/teachers");
const { ContactUserModel } = require("./model/contact");
// Calling express
const app = express();
// Express Uses
app.use(cors({ origin: "*", credentials: true }));
app.use(bodyParser.json());
app.use(express.urlencoded({ extended: false }));
// Conecting to Front End
app.use("/", express.static(path.resolve(path.join(__dirname, "public"))));
// Sign Up POST Request
app.post("/signUp", (req, res, next) => {
// Model Of The Database finding one Of them Matchby email
SignUpUserModel.findOne({ email: req.body.email }, (err, data) => {
if (err || data) {
// Making Satment for Email Cannot Match
if (data.email === req.body.email) {
//Sending Message to Fornt End With Status Of 409
res.status(409).send({
message: "Please Make Another Account User Already Exists !",
});
return;
}
} else {
// Using Bcryptjs for Generating a HASH Password
// Generating Salts For HASH password
const saltRounds = 12;
bycrypt.genSalt(saltRounds, function (err, salt) {
bycrypt.hash(req.body.password, salt, function (err, hash) {
// Making The Schema for Getting All Information From The Front End and save it
const newSignUpPerson = SignUpUserModel({
username: req.body.username,
email: req.body.email,
phone: req.body.phone,
password: hash,
confPassword: hash,
});
// Saving Sign Up User to The Data Base
newSignUpPerson.save((err, data) => {
if (!err) {
// Sending Message to Fornt End With Status Of 200
res.status(200).send({
message: "Sign Up SuccesFull !",
data,
});
} else {
res.status(405).send({
// Sending Message to Fornt End With Status Of 405
message: "User creation Failed",
});
}
});
});
});
}
});
});
// Making Login POST Request
app.post("/logIn", (req, res, next) => {
SignUpUserModel.findOne({ email: req.body.email }, (err, data) => {
if (data) {
if (data.email === req.body.email) {
// Breaking The HASH Password For Checking The User Is Valid or Not Valid by comparing the old password to the req.body.password
bycrypt.compare(req.body.password, data.password, (err, isFound) => {
if (isFound) {
var token = jwt.sign(
{
email: req.body.email,
password: req.body.password,
},
"hIHkthjUhuvfhuiyvnjy7yii9trefhon"
);
console.log(token);
res.cookie("jToken", token, {
maxAge: 86_400_000,
httpOnly: true,
});
//Sending Message to Fornt End With Status Of 200
res.status(200).send({
data: data.username + " Welcome To Our Website ! ",
});
} else {
//Sending Message to Fornt End With Status Of 405
res.status(405).send({
message: "The Password Is Incorrect !",
});
}
});
} else {
//Sending Message to Fornt End With Status Of 405
res.status(405).send({
message: "Email is Incorrect !",
});
}
} else if (
req.body.email === "[email protected]" &&
req.body.password === "admin"
) {
//Sending Message to Fornt End With Status Of 201
res.status(201).send({
message: "Syed Tariq Ahmed Admin Welcome To Admin Page !",
});
} else {
//Sending Message to Fornt End With Status Of 405
res.status(405).send({
message: "Email is Inccorect !",
});
}
});
});
// app.use((req, res, next) => {
// if (!req.cookies.jToken) {
// res.status(401).send({
// message: "Wrong Token",
// });
// return;5
// }
// jwt.verify(
// req.cookies,
// "hIHkthjUhuvfhuiyvnjy7yii9trefhon",
// (err, decodeData) => {
// if (!err) {
// console.log("hellollo");
// const issueDate = decodeData.iat * 1000;
// const nowDate = new Date().getTime();
// const diff = nowDate - issueDate; // 86400,000
// console.log(diff);
// if (diff > 300000) {
// res.status(401).send({
// message: "Token Expired",
// });
// } else {
// var token = jwt.sign(
// {
// id: decodeData.id,
// email: decodeData.email,
// password: decodeData.password,
// },
// "hIHkthjUhuvfhuiyvnjy7yii9trefhon"
// );
// res.cookie("jToken", token, {
// maxAge: 86_400_000,
// httpOnly: true,
// });
// req.body.jToken = decodeData;
// next();
// }
// } else {
// res.status(401).send({
// message: "invalid token",
// });
// }
// }
// );
// });
// Making The Admission POST Request For Adding Student
app.post("/Admission", (req, res, next) => {
AdmissionUserModel.findOne({ email: req.body.email }, (err, data) => {
if (err || data) {
if (data.email === req.body.email) {
//Sending Message to Fornt End With Status Of 405
res.status(405).send({
message: "User Already Exists Please Make Another Email ID !",
});
}
} else {
const newAdmissionPerson = AdmissionUserModel({
// Making The Schema for Getting All Information From The Front End and save it
stDname: req.body.stDname,
age: req.body.age,
email: req.body.email,
contactno: req.body.contactno,
adress: req.body.adress,
nationality: req.body.nationality,
placeofBIrth: req.body.placeofBIrth,
level: req.body.level,
});
// Saving Sign Up User to The Data Base
newAdmissionPerson.save((err, data) => {
if (!err) {
//Sending Message to Fornt End With Status Of 405
res.status(200).send({
message: "Your Form Has Been Submitted !",
data,
});
} else {
//Sending Message to Fornt End With Status Of 405
res.status(405).send({
message: "User creation Failed",
});
}
});
}
});
});
// Making Get Requests for getting all data from Database
app.get("/descdata", (req, res) => {
// Finding all DATA from Database
const data = DescModel.find({}, (err, data) => {
if (err) {
// Sending Error If error
res.send(err);
} else {
// Sending Data to the Front End
res.send(data);
}
});
});
app.get("/admin", (req, res) => {
// Finding all DATA from Database
const data = SignUpUserModel.find({}, (err, data) => {
if (err) {
// Sending Error If error
res.send(err);
} else {
// Sending Data to the Front End
res.send(data);
}
});
});
app.get("/signupdata", (req, res) => {
// Finding all DATA from Database
const data = AdmissionUserModel.find({}, (err, data) => {
if (err) {
// Sending Error If error
res.send(err);
} else {
// Sending Data to the Front End
res.send(data);
console.log(data);
}
});
});
app.get("/teacherdata", (req, res) => {
// Finding all DATA from Database
const data = TeachersUserModel.find({}, (err, data) => {
if (err) {
// Sending Error If error
res.send(err);
} else {
// Sending Data to the Front End
res.send(data);
console.log(data);
}
});
});
app.get("/descdata", (req, res) => {
// Finding all DATA from Database
const data = DescModel.find({}, (err, data) => {
if (err) {
// Sending Error If error
res.send(err);
} else {
// Sending Data to the Front End
res.send(data);
console.log(data);
}
});
});
app.get("/condata", (req, res) => {
// Finding all DATA from Database
const data = ContactUserModel.find({}, (err, data) => {
if (err) {
// Sending Error If error
res.send(err);
} else {
// Sending Data to the Front End
res.send(data);
console.log(data);
}
});
});
// Making Delete Request For Deleting data From Database
app.delete("/delete/:id", (req, res) => {
SignUpUserModel.findByIdAndRemove(req.params.id, (err, data) => {
if (!err) {
res.status(200).send({
message: "Refresh Your Page !",
});
} else {
res.status(500).send({
message: "error",
err,
});
}
});
});
app.delete("/teachdelete/:id", (req, res) => {
TeachersUserModel.findByIdAndRemove(req.params.id, (err, data) => {
if (!err) {
res.status(200).send({
message: "Refresh Your Page !",
});
} else {
res.status(500).send({
message: "error",
err,
});
}
});
});
app.put("/update/:id", (req, res) => {
SignUpUserModel.findOneAndUpdate(
{ id: req.params.id },
{
$set: {
username: req.body.username,
email: req.body.email,
phone: req.body.phone,
},
}
)
.then((data) => {
res.status(200).send({
message: "User Updated !",
});
})
.catch((err) => {
res.status(500).send({
message: err,
});
});
});
app.put("/teachupdate/:id", (req, res) => {
TeachersUserModel.findOneAndUpdate(
{ id: req.params.id },
{
$set: {
username: req.body.username,
email: req.body.email,
},
}
)
.then((data) => {
res.status(200).send({
message: "User Updated !",
});
})
.catch((err) => {
res.status(500).send({
message: err,
});
});
});
app.delete("/admidelete/:id", (req, res) => {
AdmissionUserModel.findByIdAndRemove(req.params.id, (err, data) => {
if (!err) {
res.status(200).send({
message: "Refresh Your Page !",
});
} else {
res.status(500).send({
message: "error",
err,
});
}
});
});
app.delete("/descdelete/:id", (req, res) => {
DescModel.findByIdAndRemove(req.params.id, (err, data) => {
if (!err) {
res.status(200).send({
message: "Refresh Your Page !",
});
} else {
res.status(500).send({
message: "error",
err,
});
}
});
});
app.delete("/condelete/:id", (req, res) => {
ContactUserModel.findByIdAndRemove(req.params.id, (err, data) => {
if (!err) {
res.status(200).send({
message: "Refresh Your Page !",
});
} else {
res.status(500).send({
message: "error",
err,
});
}
});
});
app.put("/descupdate/:id", (req, res) => {
DescModel.findOneAndUpdate(
{ id: req.params.id },
{
$set: {
desc: req.body.desc,
paradesc: req.body.paradesc,
},
}
)
.then((data) => {
res.status(200).send({
message: "User Updated !",
});
})
.catch((err) => {
res.status(500).send({
message: err,
});
});
});
app.put("/admiupdate/:id", (req, res) => {
AdmissionUserModel.findOneAndUpdate(
{ id: req.params.id },
{
$set: {
stDname: req.body.stDname,
email: req.body.email,
contactno: req.body.contactno,
adress: req.body.adress,
},
}
)
.then((data) => {
res.status(200).send({
message: "User Updated !",
});
})
.catch((err) => {
res.status(500).send({
message: err,
});
});
});
app.put("/Contactupdate/:id", (req, res) => {
ContactUserModel.findOneAndUpdate(
{ id: req.params.id },
{
$set: {
firstname: req.body.firstname,
email: req.body.email,
messgae: req.body.messgae,
},
}
)
.then((data) => {
res.status(200).send({
message: "User Updated !",
});
})
.catch((err) => {
res.status(500).send({
message: err,
});
});
});
app.post("/logout", (req, res, next) => {
res.cookie("jToken", "", {
maxAge: 86_400_000,
httpOnly: true,
});
res.status(200).send({
message: "Logout Succesfully !",
});
});
app.post("/desc", (req, res, next) => {
const newDesc = DescModel({
desc: req.body.desc,
paradesc: req.body.paradesc,
date: req.body.date,
});
newDesc.save((err, data) => {
if (!err) {
res.status(200).send({
message: "You Description Has Send To the Page !",
data,
});
} else {
res.status(405).send({
message: "Could'nt Send Description",
});
}
});
});
app.post("/Teachers", (req, res, next) => {
TeachersUserModel.findOne({ email: req.body.email }, (err, data) => {
if (err || data) {
if (data.email === req.body.email) {
//Sending Message to Fornt End With Status Of 405
res.status(405).send({
message: "User Already Exists Please Make Another Email ID !",
});
}
} else {
const newTeacherPerson = TeachersUserModel({
// Making The Schema for Getting All Information From The Front End and save it
teachusername: req.body.teachusername,
email: req.body.email,
password: req.body.password,
confPassword: req.body.confPassword,
});
// Saving Sign Up User to The Data Base
newTeacherPerson.save((err, data) => {
if (!err) {
//Sending Message to Fornt End With Status Of 405
res.status(200).send({
message: "Your Form Has Been Submitted !",
data,
});
} else {
//Sending Message to Fornt End With Status Of 405
res.status(405).send({
message: "User creation Failed",
});
}
});
}
});
});
app.post("/contact", (req, res, next) => {
ContactUserModel.findOne({ email: req.body.email }, (err, data) => {
if (err || data) {
if (data.email === req.body.email) {
//Sending Message to Fornt End With Status Of 405
res.status(405).send({
message: "User Already Exists Please Make Another Email ID !",
});
}
} else {
const newContactPerson = ContactUserModel({
// Making The Schema for Getting All Information From The Front End and save it
firstname: req.body.firstname,
lastname: req.body.lastname,
email: req.body.email,
messgae: req.body.messgae,
});
// Saving Sign Up User to The Data Base
newContactPerson.save((err, data) => {
if (!err) {
//Sending Message to Fornt End With Status Of 405
res.status(200).send({
message: "Your Form Has Been Submitted !",
data,
});
} else {
//Sending Message to Fornt End With Status Of 405
res.status(405).send({
message: "User creation Failed",
});
}
});
}
});
});
app.listen(port, () => {
console.log("Server is Running ", port);
});