-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (74 loc) · 1.96 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
76
require("dotenv").config();
const express = require("express");
const app = express();
const cors = require("cors");
const port = process.env.PORT || 3000;
const rateLimit = require("express-rate-limit");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
mongoose.set("strictQuery", true);
const datascheme = mongoose.model(
"codelist",
new mongoose.Schema({
datalist: Object,
updatelist: String,
Find: String,
})
);
mongoose.connect(process.env.DB_URL, {
useUnifiedTopology: true,
useNewUrlParser: true,
});
const limiter = rateLimit({
windowMs: 3000,
max: 3,
standardHeaders: true,
message: {
messge: `You requested too many time wait 3 seconds before requesting again`,
},
legacyHeaders: false,
})
app.use(cors(), bodyParser.json(), limiter);
app.get("/", async (req, res) => {
res.send({
message: "Please refer to the docs",
docs: "genshincodeslist.is-an.app/docs",
});
});
app.post("/update", async (req, res) => {
if (req.headers.authorization !== process.env.POST_SECRET_KEY) {
return res.status(400).json({ message: "Wrong Authorization Token!" });
}
datascheme.findOneAndDelete({ Find: "Web" }, async (err, data) => {
if (data) {
data = new datascheme({
datalist: req.body,
updatelist: new Date(),
Find: "Web",
});
await data.save();
return res.send("Updated as per now!");
} else {
data = data = new datascheme({
datalist: req.body,
updatelist: new Date(),
Find: "Web",
});
await data.save();
return res.send("Updated as per now!");
}
});
});
app.get("/code", async (req, res) => {
datascheme.findOne({ Find: "Web" }, async (err, data) => {
if (data) {
res.status(300).json(data.datalist)
}
})
});
app.get("*", async (req, res) => {
res.status(404).json({ message: "404 Not found" });
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}!`);
});