-
Notifications
You must be signed in to change notification settings - Fork 59
/
app.js
51 lines (45 loc) · 1.05 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 app = express();
app.use(express.static('client'));
app.use(express.json());
const goats = [
{
name: 'Phil',
fact: 'Stars in Hercules'
},
{
name: 'Billy',
fact: 'Stars in Stardust'
},
{
name: 'Mr Tumnus',
fact: 'Features in The Lion, the Witch and the Wardrobe'
},
{
name: 'Jocelyn Bell Burnell',
fact: 'Discovered pulsars'
}
];
app.get('/goat/question', function (req, resp) {
const q = req.query.goat_question;
const answers = [];
for (const goat of goats) {
if (goat.name.includes(q) || goat.fact.includes(q)) {
answers.push(goat);
}
}
resp.json(answers); // resp.send would also send as JSON
});
app.get('/random/', function (req, resp) {
const rand = Math.floor(Math.random() * goats.length);
const goat = goats[rand];
const name = goat.name;
const fact = goat.fact;
resp.json(name + ' ' + fact);
});
app.post('/goat/add', function (req, resp) {
const newGoat = req.body;
goats.push(newGoat);
resp.json(goats);
});
module.exports = app;