-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
98 lines (82 loc) · 1.73 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
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
const express = require('express');
const app = express();
app.use(express.json());
let currentUser = {
id: '123',
name: 'John Doe',
age: 54,
hairColor: 'brown',
hobbies: ['swimming', 'bicycling', 'video games'],
};
let users = [
{
id: '123',
name: 'John Doe',
age: 54,
hairColor: 'brown',
hobbies: ['swimming', 'bicycling', 'video games'],
},
{
id: '234',
name: 'Brenda Smith',
age: 33,
hairColor: 'black',
hobbies: ['golf', 'mathematics'],
},
{
id: '345',
name: 'Jane Garcia',
age: 27,
hairColor: 'blonde',
hobbies: ['biology', 'medicine', 'gymnastics'],
},
];
const products = [
{
id: '1234',
name: 'Flat-Screen TV',
price: '$300',
description: 'Huge LCD screen, a great deal',
rating: 4.5,
},
{
id: '2345',
name: 'Basketball',
price: '$10',
description: 'Just like the pros use',
rating: 3.8,
},
{
id: '3456',
name: 'Running Shoes',
price: '$120',
description: 'State-of-the-art technology for optimum running',
rating: 4.2,
},
];
app.get('/current-user', (req, res) => {
res.json(currentUser);
});
app.get('/users/:id', (req, res) => {
const { id } = req.params;
res.json(users.find((user) => user.id === id));
});
app.post('/users/:id', (req, res) => {
const { id } = req.params;
const { user: updatedUser } = req.body;
users = users.map((user) => (user.id === id ? updatedUser : user));
res.json(users.find((user) => user.id === id));
});
app.get('/users', (req, res) => {
res.json(users);
});
app.get('/products/:id', (req, res) => {
const { id } = req.params;
res.json(products.find((product) => product.id === id));
});
app.get('/products', (req, res) => {
res.json(products);
});
app.listen(8080, () => {
console.log('Server is listening on port 8080');
});