-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
65 lines (57 loc) · 1.38 KB
/
server.js
File metadata and controls
65 lines (57 loc) · 1.38 KB
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
const express = require("express");
const path = require("path");
const app = express();
app.use(express.json());
app.use(express.static(path.join(__dirname, "public")));
const people = [
{
id: 0,
name: "John",
email: "john@asdf.com",
puppies: [
{ name: "Goofy", weight: 2 },
{ name: "Lucky", weight: 2.4 },
],
},
{
id: 1,
name: "Anna",
email: "anna@asdf.com",
puppies: [{ name: "Brian", weight: 2 }],
},
];
app.get("/api/people", (_req, res) => {
res.json(people);
});
app.get("/api/people/:id", (req, res) => {
let target = null;
for (const person of people) {
if (person.id == req.params.id) {
target = person;
}
}
res.json(target);
});
app.post("/api/people/:id/puppies", (req, res) => {
let target = null;
for (const person of people) {
if (person.id === req.params.id) {
target = person;
}
}
target.puppies.push(req.body);
return res.json(target);
});
app.get("/api/people/:id/puppy-count", (req, res) => {
let target = null;
for (const person of people) {
if (person.id == req.params.id) {
target = person;
}
}
return res.json({ count: target.puppies });
});
const PORT = 3005;
app.listen(PORT, () => {
console.log("listening on", PORT);
});