-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.js
More file actions
80 lines (70 loc) · 2.33 KB
/
solution.js
File metadata and controls
80 lines (70 loc) · 2.33 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
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";
//Add your own bearer token from the previous lesson.
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) => {
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) => {
try {
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) => {
const searchId = req.body.id;
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) => {
const searchId = req.body.id;
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;
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}`);
});