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