-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
81 lines (67 loc) · 1.96 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
const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");
const app = express();
const PORT = process.env.PORT || 5000;
// Middleware
app.use(cors());
app.use(express.json());
// Connect to MongoDB
mongoose.connect("mongodb://localhost:27017/data", {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const connection = mongoose.connection;
connection.once("open", () => {
console.log("Connected to MongoDB database");
});
// Define leaderboard schema
const User=require('./models/user')
const Admin=require('./models/admin')
const Order=require('./models/order')
const Product=require('./models/product')
// API to fetch leaderboard data
app.get("/getuser", async (req, res) => {
try {
const data = await User.find();
res.json(data);
console.log("Done")
} catch (err) {
console.error(err); // Print any errors to the console for debugging
res.status(500).json({ message: "Internal Server Error" });
}
});
app.get("/getadmin", async (req, res) => {
try {
const data = await Admin.find();
res.json(data);
console.log("Done")
} catch (err) {
console.error(err); // Print any errors to the console for debugging
res.status(500).json({ message: "Internal Server Error" });
}
});
app.get("/getproduct", async (req, res) => {
try {
const data = await Product.find();
res.json(data);
console.log("Done")
} catch (err) {
console.error(err); // Print any errors to the console for debugging
res.status(500).json({ message: "Internal Server Error" });
}
});
app.get("/getorder", async (req, res) => {
try {
const data = await Order.find();
res.json(data);
console.log("Done")
} catch (err) {
console.error(err); // Print any errors to the console for debugging
res.status(500).json({ message: "Internal Server Error" });
}
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});