-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
31 lines (27 loc) · 989 Bytes
/
index.js
File metadata and controls
31 lines (27 loc) · 989 Bytes
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
import express from "express";
import axios from "axios";
import bodyParser from "body-parser";
const app = express();
const port = process.env.PORT || 3000;
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended:true}))
app.get("/",(req,res)=>{
res.render("index.ejs");
});
app.post("/submit",async (req,res)=>{
try{
if(req.body.name != ""){
const response = await axios.get("https://v2.jokeapi.dev/joke/Programming?type=twopart&blacklistFlags=nsfw,religious,political,racist,sexist,explicit");
const result = response.data;
res.render("index.ejs",{question:result.setup, answer:result.delivery})
} else {
res.render("index.ejs",{error:"Please enter your name"})
}
} catch(error){
console.error(error.response.data);
res.sendStatus(404).send(error.response.data);
}
});
app.listen(port,()=>{
console.log(`Server listening on port ${port} ...`)
});