-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
55 lines (46 loc) · 1.37 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
52
53
54
55
import express from 'express';
import bodyParser from 'body-parser';
import request from 'request';
const app = express()
app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs')
app.get('/', (req, res) => {
res.render('index', {fact: null, error: null});
})
app.post('/', (req, res) => {
let url = `https://catfact.ninja/fact`
request(url, (err, response, body) => {
if(err){
res.render('index', {fact: null, error: 'Error, please try again'});
} else {
let resp = JSON.parse(body)
if(resp.fact == undefined){
res.render('index', {fact: null, error: 'Error, please try again'});
} else {
let randomFact = resp.fact;
console.log('Random Fact: ', randomFact);
res.render('index', {fact: randomFact, error: null});
}
}
});
})
app.post('/getRandomCatFact', (req, res) => {
let url = `https://catfact.ninja/fact`
request(url, (err, response, body) => {
if(err){
console.log('Error, please try again');
} else {
let resp = JSON.parse(body)
if(resp.fact == undefined){
console.log('Error with response, please try again');
} else {
let randomFact = resp.fact;
console.log('Random Fact: ', randomFact);
}
}
});
})
app.listen(8080, () => {
console.log('App listening on port 8080!')
})