-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
75 lines (65 loc) · 1.89 KB
/
index.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
import express from "express";
import cors from "cors";
import helmet from "helmet";
import { mongoose } from "mongoose";
import routes from "./src/routes/api.js";
import rateLimit from "express-rate-limit";
const app = express();
import {
MONGODB_CONNECTION,
PORT,
MAX_JSON_SIZE,
URL_ENCODED,
REQUEST_LIMIT_NUMBER,
REQUEST_LIMIT_TIME,
} from "./src/config/config.js";
// port
const port = PORT || 5000;
// middlware
app.use(cors());
app.use(helmet());
app.use(express.json({ limit: MAX_JSON_SIZE }));
app.use(express.urlencoded({ extended: URL_ENCODED }));
const limiter = rateLimit({
windowMs: REQUEST_LIMIT_TIME,
max: REQUEST_LIMIT_NUMBER,
});
app.use(limiter);
// MongoDB connection
mongoose
.connect(MONGODB_CONNECTION, { autoIndex: true })
.then(() => {
console.log("Database Connected");
// Accessing MongoDB resultCollection after the connection is established
const resultCollection = mongoose.connection.db.collection("result");
// Assuming 'identifier' is the identifier you want to search for
app.get("/api/result/:identifier", async (req, res) => {
const identifier = req.params.identifier;
try {
const result = await resultCollection.findOne({
[identifier]: { $exists: true },
});
if (result) {
res.status(200).json(result[identifier]);
} else {
res.status(404).json({ error: "Result not found" });
}
} catch (error) {
console.error("Error fetching result:", error);
res.status(500).json({ error: "Internal server error" });
}
});
})
.catch((err) => {
console.error("Database connection error:", err);
});
// API routes
app.use("/api", routes);
// start up basic route
app.get("/", async (req, res) => {
res.send("Welcome to the backend of SPI");
});
// listing port
app.listen(port, () => {
console.log(`http://localhost:${port}`, port);
});