-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
87 lines (79 loc) · 3.42 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
const express = require('express')
const app = express()
const cors = require('cors')
app.set('port', process.env.port || 3000)
app.use(express.json())
app.use(cors())
app.locals.title = 'Adventure Sandals'
app.locals.sandals = [
{
id: 1,
name: 'Bedrock Cairn',
weight: '8.5 oz.',
price: 105,
hasArchSupport: false,
imageLink: 'https://cdn.shopify.com/s/files/1/1132/3434/products/BEDROCK_CAIRN_Maroon_400x.jpg?v=1595966700',
description: 'Minimalist, lightweight sandals with superior footbed traction and a unique carriage hug your foot in mucky and slick conditions when you want top-notch security and an overwhelming sense of freedom.'
},
{
id: 2,
name: 'Chaco Mega Z/Cloud',
weight: '1 lb. 5 oz.',
price: 115,
hasArchSupport: true,
imageLink: 'https://a248.e.akamai.net/f/248/9086/10h/wolverine-o.scene7.com/is/image/WolverineWorldWide/CHAW-JCH107758-050919-S20-000?wid=584&hei=484&op_usm=0.5,1&qlt=70&fmt=png-alpha',
description: 'Ample arch support and a durable midsole make the Mega Z/Cloud the only sandal in the test built for high-mileage pursuits and heavy packs. Built like a tank and if you live in Colorado, owning a pair of these is literally required.'
},
{
id: 3,
name: 'Teva Original Universal',
weight: '13.5 oz.',
price: 50,
hasArchSupport: true,
imageLink: 'https://www.teva.com/dw/image/v2/AAFF_PRD/on/demandware.static/-/Sites-masterCatalogTeva/default/dw8acd592e/images/grey/1004006-RTML_1.jpg?sw=1020&sh=1020&sm=fit',
description: 'From a Grand Canyon raft in 1984 to docks, stoops, and sidewalks around the world, one of Teva’s very first sandal stands as a testament to timeless comfort and utilitarian style. Teva pretty much invented the adventure sandal with this revolution in footwear.'
},
{
id: 4,
name: 'No-name gas station flip-flop',
weight: '4 oz.',
price: 2.99,
hasArchSupport: false,
imageLink: 'https://www.inbop.com/wp-content/uploads/2017/07/Bulk-Childrens-Flip-Flops..jpg',
description: 'Adventure is subjective.'
}
]
app.get('/', (request, response) => {
response.send('Is it really an adventure without a pair of Adventure Sandals?')
})
app.get('/sandals', (request, response) => {
const sandals = app.locals.sandals
response.status(200).json({ sandals })
})
app.get('/sandals/:id', (request, response) => {
const { id } = request.params
const sandal = app.locals.sandals.find(sandal => sandal.id === +id)
if (!sandal) {
return response.status(404).json({
errorMessage: `A sandal with an id of ${id} could not be found`
})
}
response.status(200).json({ sandal })
})
app.post('/sandals', (request, response) => {
const requiredProps = ['name', 'weight', 'price', 'hasArchSupport', 'imageLink', 'description']
const receivedProps = Object.keys(request.body)
for (let prop of requiredProps) {
if (!receivedProps.includes(prop)) {
return response.status(422).json({
errorMessage: `Cannot POST: Missing property '${prop}' in request. Please include a ${typeof prop} with a key of ${prop}`
})
}
}
const newSandal = { id: app.locals.sandals.length + 1, ...request.body } // Should this typically be a 'random' Id like Date.now() instead?
app.locals.sandals.push(newSandal)
response.status(201).json(newSandal)
})
app.listen(app.get('port'), () => {
console.log(`${app.locals.title} is now listening on http://localhost:${app.get('port')}!`)
})