-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (97 loc) · 2.45 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
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
require("dotenv").config();
const express = require("express");
const app = express();
const mongoose = require("mongoose");
app.use((req, res, next) => {
res.header({
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "PUT, POST, GET, DELETE",
"Access-Control-Allow-Headers": "*",
});
next();
});
app.use(express.json());
app.set('json spaces', 2)
// Mongo URI
mongoose
.connect(`${process.env.DB_URL}`)
.then((m) => {
console.log("🟢 SUCCESS - Database Connected.");
})
.catch((e) => {
console.log("🔴 FAILED - Database Connection Error!");
});
const saveSchema = new mongoose.Schema({
name: String,
data: String,
});
const SaveData = new mongoose.model("data", saveSchema);
app.post("/save", (req, res) => {
const saveData = new SaveData({
name: req.body.name,
data: req.body.data,
});
saveData
.save()
.then((r) => {
res.send("[*] data has been saved successfully.");
})
.catch((e) => {
res.send(e);
});
});
app.get("/save/:search", (req, res) => {
const key = req.params.search;
SaveData.find({ name: key }).select(["-_id", "-__v"])
.then((d) => {
if (d?.length === 0) {
res.send("[*] no match found!");
} else {
res.send(d);
}
})
.catch((e) => {
res.send(e);
});
});
app.get("/", (req, res) => {
res.send(
"***********\n\n[*] /save or /code :: Show saved data\n[*] /save/key :: Found the key in database.\n[*] /save/data/{title}/{data} :: Save the title and data to database.\n[*] Alt+F7 :: Clears the CMD history.\n\n***********"
);
});
app.get("/save", (req, res) => {
SaveData.find().select(["-_id", "-__v"])
.then((o) => {
res.json(o);
})
.catch((e) => {
res.send(e);
});
});
app.get("/save/data/:title/:data", (req, res) => {
const saveData = new SaveData({
name: req.params.title,
data: req.params.data,
});
saveData
.save()
.then((r) => {
res.send("[*] data has been saved successfully.");
})
.catch((e) => {
res.send("[x] something went wrong.");
});
});
app.get("/code", (req, res) => {
SaveData.find()
.then((o) => {
res.json(o);
})
.catch((e) => {
res.send(e);
});
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log("Server Active on PORT 3000");
});