-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
92 lines (75 loc) · 1.75 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
const express = require('express');
const cors = require('cors')
const app = express();
app.use(express.json());
app.use(cors());
let currentUser = {
name: 'John Doe',
age: 54,
hairColor: 'brown',
hobbies: ['swimming', 'bicycling', 'video games'],
};
let users = [{
id: '01',
name: 'John Doe',
age: 54,
hairColor: 'brown',
hobbies: ['swimming', 'bicycling', 'video games'],
}, {
id: '02',
name: 'Brenda Smith',
age: 33,
hairColor: 'black',
hobbies: ['golf', 'mathematics'],
}, {
id: '03',
name: 'Jane Garcia',
age: 27,
hairColor: 'blonde',
hobbies: ['biology', 'medicine', 'gymnastics'],
}];
const products = [{
id: "01",
name: 'Flat-Screen TV',
price: '$300',
description: 'Huge LCD screen, a great deal',
rating: 4.5,
}, {
id: "02",
name: 'Basketball',
price: '$10',
description: 'Just like the pros use',
rating: 3.8,
}, {
id: "03",
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');
});