-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
90 lines (79 loc) · 2.96 KB
/
index.js
File metadata and controls
90 lines (79 loc) · 2.96 KB
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
import express from "express";
import axios from "axios";
import bodyParser from "body-parser";
const app = express();
const port = 3000;
const API_URL = "https://secrets-api.appbrewery.com";
const yourBearerToken = "08f3026d-9c6c-4d88-a3b2-c579dc106247";
const config = {
headers: { Authorization: `Bearer ${yourBearerToken}` },
};
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", (req, res) => {
res.render("index.ejs", { content: "Waiting for data..." });
});
app.post("/get-secret", async (req, res) => {//post request using axios because we are giving input through ui dropdown
const searchId = req.body.id;
try {
const result = await axios.get(API_URL + "/secrets/" + searchId, config);
res.render("index.ejs", { content: JSON.stringify(result.data) });
} catch (error) {
res.render("index.ejs", { content: JSON.stringify(error.response.data) });
}
});
app.post("/post-secret", async (req, res) => {
// axios to POST the data from req.body to the secrets api servers.
const searchId = req.body.id;
const secrett = req.body.secret;
const scoree = req.body.score;
console.log(req.body);
const newdata = {id : searchId, secret : secrett, score : scoree};
try {
const result = await axios.post(API_URL + "/secrets", newdata, config);
// const result = await axios.post(API_URL + "/secrets", req.body, config);
res.render("index.ejs", { content: JSON.stringify(result.data) });
} catch (error) {
res.render("index.ejs", { content: JSON.stringify(error.response.data) });
}
});
app.post("/put-secret", async (req, res) => {//needds to enter all fields
const searchId = req.body.id;
// axios to PUT the data from req.body to the secrets api servers.
try {
const result = await axios.put(
API_URL + "/secrets/" + searchId,
req.body,
config
);
res.render("index.ejs", { content: JSON.stringify(result.data) });
} catch (error) {
res.render("index.ejs", { content: JSON.stringify(error.response.data) });
}
});
app.post("/patch-secret", async (req, res) => {//can be modify single data
const searchId = req.body.id;
// Use axios to PATCH the data from req.body to the secrets api servers.
try {
const result = await axios.patch(
API_URL + "/secrets/" + searchId,
req.body,
config
);
res.render("index.ejs", { content: JSON.stringify(result.data) });
} catch (error) {
res.render("index.ejs", { content: JSON.stringify(error.response.data) });
}
});
app.post("/delete-secret", async (req, res) => {
const searchId = req.body.id;
// Use axios to DELETE the item with searchId from the secrets api servers.
try {
const result = await axios.delete(API_URL + "/secrets/" + searchId, config);
res.render("index.ejs", { content: JSON.stringify(result.data) });
} catch (error) {
res.render("index.ejs", { content: JSON.stringify(error.response.data) });
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});