-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
91 lines (74 loc) · 1.97 KB
/
server.js
File metadata and controls
91 lines (74 loc) · 1.97 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
const express = require('express');
const app = express();
app.use(express.json());
let currentUser = {
id: '123',
name: 'Shan Kumar',
age: 48,
hairColor: 'brown',
hobbies: ['bicycling', 'reading', 'swimming']
};
let users = [{
id: '123',
name: 'Shan Kumar',
age: 48,
hairColor: 'brown',
hobbies: ['bicycling', 'reading', 'swimming']
}, {
id: '124',
name: 'Nadee Sansari',
age: 28,
hairColor: 'black',
hobbies: ['dancing', 'reading', 'movie watching']
}];
let products = [{
productId: '1234',
name: 'Piano',
price: '$450',
description: 'Used Piano, with great condition',
rating: 7.8,
}, {
productId: '1235',
name: 'Iphone 14',
price: '$200',
description: 'Used Iphone, with great condition',
rating: 5.8,
}, {
productId: '12341',
name: 'TV',
price: '$150',
description: 'Used TV, with great condition',
rating: 3.8,
}];
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:updateUser } = req.body;
users = users.map(user => user.id === id ? updateUser: null );
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.productId === id));
});
app.post('/products/:id', (req, res) => {
const { id } = req.params;
const { product:updateProduct } = req.body;
products = products.map(product => product.productId === id ? updateProduct: null );
res.json(products.find(product => product.productId === id));
});
app.get('/products', (req, res) => {
res.json(products);
});
app.listen(8080, () => {
console.log('Server is listening on port 8080');
});