I don’t know if i am the only one, but i think really often codeacademy give to me bad instructions.
For example here in this charapter:
MIDDLEWARE
Route-Level app.use() - Single Path ( BackEnd )
It explain:
You can now remove the duplicate checking logic from all /beans/:beanName routes. To make sure that all your routes still work if we remove const beanName = req.params.beanName; from them, make sure that you use req.beanName any place where you need to access the bean by name. For instance, inside app.delete, you’ll have to change
jellybeanBag[beanName] = null;
to
jellybeanBag[req.beanName] = null;
Check your routes to make sure that they use req.beanName.
But in the code is everything different from wrote: it’s like that:
app.get('/beans/:beanName', (req, res, next) => {
res.send(req.bean);
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;
req.bean.number += numberOfBeans;
res.send(req.bean);
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 (req.bean.number < numberOfBeans) {
return res.status(400).send('Not enough beans in the jar to remove!');
}
req.bean.number -= numberOfBeans;
I really don’t know if it’s my fault, but many times asking for something really different in the code.