-
-
Save bcuz/2aca7ee79b3f22c8a1ddca6ac37ff568 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const app = express(); | |
app.use(express.static('public')); | |
const PORT = process.env.PORT || 4001; | |
const jellybeanBag = { | |
mystery: { | |
number: 4 | |
}, | |
lemon: { | |
number: 5 | |
}, | |
rootBeer: { | |
number: 25 | |
}, | |
cherry: { | |
number: 3 | |
}, | |
licorice: { | |
number: 1 | |
} | |
}; | |
// Logging Middleware | |
app.use((req, res, next) => { | |
console.log(`${req.method} Request Received`); | |
next(); | |
}); | |
// Add your code below: | |
app.use('/beans/:beanName', (req, res, next) => { | |
const beanName = req.params.beanName; | |
if (!jellybeanBag[beanName]) { | |
console.log('Response Sent'); | |
return res.status(404).send('Bean with that name does not exist'); | |
} | |
}); | |
app.get('/beans/', (req, res, next) => { | |
res.send(jellybeanBag); | |
console.log('Response Sent'); | |
}); | |
app.post('/beans/', (req, res, next) => { | |
let bodyData = ''; | |
req.on('data', (data) => { | |
bodyData += data; | |
}); | |
req.on('end', () => { | |
const body = JSON.parse(bodyData); | |
const beanName = body.name; | |
if (jellybeanBag[beanName] || jellybeanBag[beanName] === 0) { | |
return res.status(400).send('Bean with that name already exists!'); | |
} | |
const numberOfBeans = Number(body.number) || 0; | |
jellybeanBag[beanName] = { | |
number: numberOfBeans | |
}; | |
res.send(jellybeanBag[beanName]); | |
console.log('Response Sent'); | |
}); | |
}); | |
app.get('/beans/:beanName', (req, res, next) => { | |
res.send(jellybeanBag[beanName]); | |
console.log('Response Sent'); | |
}); | |
app.post('/beans/:beanName/add', (req, res, next) => { | |
let bodyData = ''; | |
req.on('data', (data) => { | |
bodyData += data; | |
}); | |
req.on('end', () => { | |
const numberOfBeans = Number(JSON.parse(bodyData).number) || 0; | |
jellybeanBag[beanName].number += numberOfBeans; | |
res.send(jellybeanBag[beanName]); | |
console.log('Response Sent'); | |
}); | |
}); | |
app.post('/beans/:beanName/remove', (req, res, next) => { | |
let bodyData = ''; | |
req.on('data', (data) => { | |
bodyData += data; | |
}); | |
req.on('end', () => { | |
const numberOfBeans = Number(JSON.parse(bodyData).number) || 0; | |
if (jellybeanBag[beanName].number < numberOfBeans) { | |
return res.status(400).send('Not enough beans in the jar to remove!'); | |
} | |
jellybeanBag[beanName].number -= numberOfBeans; | |
res.send(jellybeanBag[beanName]); | |
console.log('Response Sent'); | |
}); | |
}); | |
app.delete('/beans/:beanName', (req, res, next) => { | |
jellybeanBag[beanName] = null; | |
res.status(204).send(); | |
console.log('Response Sent'); | |
}); | |
app.listen(PORT, () => { | |
console.log(`Server is listening on port ${PORT}`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment