-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
51 lines (37 loc) · 1.25 KB
/
app.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
const express = require("express");
const https = require("https");
const app = express();
app.set("view engine", "ejs");
app.use(express.static("public"));
app.get("/", function(req, res) {
const url = "https://v2.jokeapi.dev/joke/Programming";
https.get(url, function(response) {
response.on("data", function(data) {
const jokePattern = JSON.parse(data);
let firstPart;
if (jokePattern.type === "twopart") {
firstPart = JSON.stringify(jokePattern.setup);
firstPart = firstPart.substring(1, firstPart.length - 1);
let secondPart = JSON.stringify(jokePattern.delivery);
secondPart = secondPart.substring(1, secondPart.length - 1);
res.render("index", {
firstPartInIndexEjs: firstPart,
secondPartInIndexEjs: secondPart
});
} else {
firstPart = JSON.stringify(jokePattern.joke);
firstPart = firstPart.substring(1, firstPart.length - 1);
res.render("index", {
firstPartInIndexEjs: firstPart,
secondPartInIndexEjs: ""
});
}
});
});
});
app.post("/", function(req, response) {
response.redirect("/");
});
app.listen(process.env.PORT || 80, function() {
console.log("Server is running on port 80.");
})