-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
145 lines (110 loc) · 2.6 KB
/
server.js
File metadata and controls
145 lines (110 loc) · 2.6 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const PORT = 5150;
const RESPONSE_DELAY = 2000;
const delay = (timeout) =>
new Promise((resolve) => setTimeout(resolve, timeout));
let petIdState = 42;
const generateId = () => petIdState++;
const petList = [
{
petId: generateId(),
petName: 'Gosho',
age: 2,
notes: 'White fur, very soft.',
kind: 1,
healthProblems: false,
addedDate: '2022-10-31',
},
{
petId: generateId(),
petName: 'Pesho',
age: 5,
notes: undefined,
kind: 2,
healthProblems: false,
addedDate: '2022-10-25',
},
{
petId: generateId(),
petName: 'Kenny',
age: 1,
notes: "Doesn't speak. Has the sniffles.",
kind: 3,
healthProblems: true,
addedDate: '2022-10-27',
},
];
const petKinds = [
{ displayName: 'Cat', value: 1 },
{ displayName: 'Dog', value: 2 },
{ displayName: 'Parrot', value: 3 },
];
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.get('/pet/kinds', async (req, res) => {
await delay(RESPONSE_DELAY);
res.json(petKinds);
});
app.get('/pet/all', async (req, res) => {
await delay(RESPONSE_DELAY);
res.json(
petList.map((pet) => ({
petId: pet.petId,
petName: pet.petName,
addedDate: pet.addedDate,
kind: pet.kind,
}))
);
});
app.get('/pet/:petId', async (req, res) => {
await delay(RESPONSE_DELAY);
const petId = Number(req.params.petId);
const pet = petList.find((pet) => pet.petId === petId);
if (pet) {
res.json(pet);
} else {
res.sendStatus(404);
}
});
app.post('/pet', async (req, res) => {
await delay(RESPONSE_DELAY);
const pet = {
...req.body,
petId: generateId(),
};
petList.push(pet);
res.json(pet);
});
app.put('/pet/:petId', async (req, res) => {
await delay(RESPONSE_DELAY);
const petId = Number(req.params.petId);
const petIndex = petList.findIndex((pet) => pet.petId === petId);
if (petIndex === -1) {
res.sendStatus(404);
return;
}
const newPet = { ...req.body, petId };
petList[petIndex] = newPet;
res.json(newPet);
});
app.delete('/pet/:petId', async (req, res) => {
await delay(RESPONSE_DELAY);
const petId = Number(req.params.petId);
const petIndex = petList.findIndex((pet) => pet.petId === petId);
if (petIndex === -1) {
res.sendStatus(404);
return;
}
const pet = petList[petIndex];
petList.splice(petIndex, 1);
res.json(pet);
});
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}.`);
});