-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.js
48 lines (43 loc) · 1.3 KB
/
solution.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
import express from "express";
import bodyParser from "body-parser";
import axios from "axios";
const app = express();
const port = 3000;
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", async (req, res) => {
try {
const response = await axios.get("https://bored-api.appbrewery.com/random");
const result = response.data;
console.log(result);
res.render("solution.ejs", { data: result });
} catch (error) {
console.error("Failed to make request:", error.message);
res.render("solution.ejs", {
error: error.message,
});
}
});
app.post("/", async (req, res) => {
try {
console.log(req.body);
const type = req.body.type;
const participants = req.body.participants;
const response = await axios.get(
`https://bored-api.appbrewery.com/filter?type=${type}&participants=${participants}`
);
const result = response.data;
console.log(result);
res.render("solution.ejs", {
data: result[Math.floor(Math.random() * result.length)],
});
} catch (error) {
console.error("Failed to make request:", error.message);
res.render("solution.ejs", {
error: "No activities that match your criteria.",
});
}
});
app.listen(port, () => {
console.log(`Server running on port: ${port}`);
});