-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
67 lines (59 loc) · 1.23 KB
/
server.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
56
57
58
59
60
61
62
63
64
65
66
67
const express = require("express");
const app = express();
app.use(express.json());
// Before the other routes
app.use(express.static("build"));
const catList = [
{
id: 1,
name: "Dimitrij",
breed: "Korat",
color: "dark grey",
age: 6,
sex: "Male",
image: "https://cataas.com/cat",
},
{
id: 2,
name: "Yuliya",
breed: "Ragdoll",
color: "white/brown",
age: 4,
sex: "Female",
image: "https://cataas.com/cat",
},
{
id: 3,
name: "Igor",
breed: "Ragamuffin",
color: "white/grey",
age: 5,
sex: "Male",
image: "https://cataas.com/cat",
},
{
id: 4,
name: "Katja",
breed: "Khao Manee",
color: "white",
age: 1,
sex: "Female",
image: "https://cataas.com/cat",
},
];
app.get("/api/cats", (req, res) => {
const singleCat = catList[Math.floor(Math.random() * catList.length)];
res.send(singleCat);
});
app.post("/api/cats", (req, res) => {
const data = req.body;
data.id = catList.length + 1;
catList.push(data);
res.send(data);
});
// After all other routes
app.get("*", (req, res) => {
res.sendFile("build/index.html");
});
const port = process.env.PORT || 8080;
app.listen(port, () => console.log(`listening on port ${port}`));