-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (93 loc) · 3.16 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
import express from "express";
import axios from "axios";
import bodyParser from "body-parser";
import env from "dotenv";
const app = express();
const port = 3000;
const API_URL = "https://secrets-api.appbrewery.com";
// HINTs: Use the axios documentation as well as the video lesson to help you.
// https://axios-http.com/docs/post_example
// Use the Secrets API documentation to figure out what each route expects and how to work with it.
// https://secrets-api.appbrewery.com/
//TODO 1: Add your own bearer token from the previous lesson.
const yourBearerToken = process.env.BEARER_TOKEN;
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;
console.log(req.body);
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) => {
// TODO 2: Use axios to POST the data from req.body to the secrets api servers.
try {
const result = await axios.get(
API_URL + "/secrets",
{
secret: req.body.secret,
score: req.body.score,
},
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;
// TODO 3: Use axios to PUT the data from req.body to the secrets api servers.
try {
const result = await axios.get(
API_URL + "/secrets" + searchId,
{
secret: req.body.secret,
score: req.body.score,
},
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;
// TODO 4: Use axios to PATCH the data from req.body to the secrets api servers.
try {
const result = await axios.get(
API_URL + "/secrets" + searchId,
{
secret: req.body.secret,
score: req.body.score,
},
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;
// TODO 5: Use axios to DELETE the item with searchId from the secrets api servers.
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.listen(port, () => {
console.log(`Server is running on port ${port}`);
});