-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
142 lines (96 loc) · 3.18 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
require("dotenv").config();
const express = require("express");
const app = express();
const path = require("path");
const buildPath = path.join(__dirname, "build");
const cors = require("cors");
const bodyParser = require("body-parser");
// Middleware
app.use(express.json());
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(buildPath))
// Importing the database connection
const connectDB = require("./config/db");
//Importing the routes
const random = require("./routes/random");
const category = require("./routes/category");
// Connecting to the database
connectDB();
// Intialising the model
const Riddle = require("./model/riddle");
// Intialising the routes
app.use("/random", random);
app.use("/category", category);
let numbers = [];
// Play By Category Route ( /categoryPlay/:category ) code written on app.js (not good practice).
app.get("/categoryplay/:category", async (req, res) => {
console.log(req.params);
const category = req?.params?.category;
if (category) {
let number = 0;
const count = await Riddle?.countDocuments({ Category: category }); // Count the number of documents in the collection
try {
const result = await Riddle?.aggregate([
{ $match: { Category: category } },
{ $sample: { size: 1 } },
]);
number = result[0].No;
if (numbers.length === count) {
numbers.splice(0, count - 1);
console.log("The array has been reset");
return res.redirect(`/CategoryPlay/${category}`);
}
// check if the number is in the array
if (numbers.includes(number)) {
console.log("This number is already in the array");
return res.redirect(`/CategoryPlay/${category}`);
} else {
numbers.push(number);
res.json(result);
}
console.log(numbers);
} catch (err) {
console.error(err);
res.status(500).json({ error: "Internal Server Error" });
}
} else {
console.log("No category provided");
window.location.href = "/";
}
});
app.get("/", (req, res) => {
res.sendFile(path.join(buildPath, "index.html"));
});
app.post("/create", async (req, res) => {
console.log(req.body);
if (!req.body.question || !req.body.answer || !req.body.category) {
return res.status(400).json({ error: "Please provide a question and an answer and a question" });
}
// Add a custom riddle to the database
// Get the current count of documents in the collection and add 1 to it
const count = await Riddle?.countDocuments();
const number = count + 1;
const riddle = new Riddle({
No: number,
Question: req.body.question || "No question provided",
Answer: req.body.answer || "No answer provided",
Category: req.body.category || "custom",
});
try {
await riddle.save();
console.log("Riddle saved");
res.redirect("/");
} catch (err) {
console.error(err);
res.status(500).json({ error: "Internal Server Error" });
}
});
app.get("*", (req, res) => {
res.sendFile(path.join(buildPath, "index.html"));
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server is running on ${port}`);
});