-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
34 lines (27 loc) · 847 Bytes
/
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
import express from "express";
import bodyParser from "body-parser";
import axios from "axios";
import pkg from "sv443-joke-api";
const app = express();
const port = 3000;
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", async (req, res) => {
res.render("index.ejs");
});
app.post("/", async (req, res) => {
try {
const response = await axios.get('https://official-joke-api.appspot.com/random_joke');
const result = response.data;
// console.log(result);
res.render("index.ejs", { setup: result.setup, punchline:result.punchline });
} catch (error) {
console.error("Failed to make request:", error.message);
res.render("index.ejs", {
error: error.message,
});
}
});
app.listen(port, () => {
console.log(`Server running on port: ${port}`);
});