-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
116 lines (98 loc) Β· 2.66 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
import express from "express";
import cors from "cors";
import mongoose from "mongoose";
import Quiz from "./model/quiz.js";
import User from "./model/users.js";
import studentAnswers from "./model/studentAnswers.js";
import jwt from "jsonwebtoken";
const secretKey = "your-secret-key";
const app = express();
const uri = "mongodb://127.0.0.1:27017/Quiz";
(async () => {
await mongoose.connect(uri);
})();
app.use(cors({ origin: true }));
app.use(cors());
app.use(express.json());
app.get("/data", async (req, res) => {
try {
const quizzes = await Quiz.find();
res.json(quizzes);
} catch (error) {
res.status(500).json({ error: "Internal Server Error" });
}
});
app.post("/login", async (req, res) => {
try {
const { email, password } = req.body;
const userFound = await User.findOne({ email: email });
if (userFound && userFound.password === password) {
const token = jwt.sign(
{
email: userFound.email,
userId: userFound._id,
fullname: userFound.fullname,
role: userFound.role,
},
secretKey,
{
expiresIn: "1h",
}
);
res.json({ message: "Login successful", token });
} else {
res.status(401).json({ message: "Invalid credentials" });
}
} catch (error) {
res.status(500).json({ message: "Internal Server Error" });
}
});
app.post("/studentAnswers", async (req, res) => {
try {
await studentAnswers.create(req.body);
res.json({ message: "Added successfuly" });
} catch (error) {
res.status(500).json({ message: "Internal Server Error" });
}
});
app.post("/addqcm", async (req, res) => {
try {
const newqcm = req.body;
await Quiz.create(newqcm);
res.status(200).send({ message: true });
} catch (error) {
res.status(500).send({ message: false });
}
});
app.post("/getUserToken", (req, res) => {
const token = req.body.token;
jwt.verify(token, secretKey, (error, userData) => {
if (error) {
} else {
res.send({
fullname: userData.fullname,
role: userData.role,
userId: userData.userId,
});
}
});
});
app.delete("/deleteqcm", async (req, res) => {
const qcmid = req.body.qcmid;
await Quiz.deleteOne({ _id: qcmid });
res.send({ message: true });
});
app.get("/responseById/:id", async (req, res) => {
const qcms = await studentAnswers.find(
{ userId: req.params.id },
{ quizId: 1, _id: 0 }
);
res.send(qcms);
});
app.get("/getresponses", async (req, res) => {
const responses = await studentAnswers.find();
res.send(responses);
});
app.listen(5000, () => {
console.log("Listening at the port : http:/localhost/5000");
});