FAQ: Router Parameters - Review


#1

This community-built FAQ covers the “Review” exercise from the lesson “Router Parameters”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn Express

FAQs on the exercise Review

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

This is ridiculous. Everything is the same, I went even as far as to copy-paste the code from the spice exercise and then refactor it to this exercise. Still, it is still not good enough. “Did you sent the same error resposne for a nonexistent spice?” my shiny behind…

const express = require(‘express’);
const app = express();
const bodyParser = require(‘body-parser’);

app.use(express.static(‘public’));

const PORT = process.env.PORT || 4001;

const vendingMachine = [
{
id: 1,
name: ‘Gum’,
price: 1.25,
},
{
id: 7,
name: ‘Bag of chips’,
price: 3.50,
},
{
id: 23,
name: ‘cumin’,
price: .75,
}
];

let nextSnackId = 24;

app.use(bodyParser.json());

// Add your code here:
app.param(‘snackId’, (req, res, next, id) => {
const snackIndex = vendingMachine.findIndex(snack => {
return snack.id === Number(id);
});
if (snackIndex !== -1) {
req.snackIndex = snackIndex;
next()
} else {
res.sendStatus(404);
}
});

app.get(’/snacks/’, (req, res, next) => {
res.send(vendingMachine);
});

app.post(’/snacks/’, (req, res, next) => {
const newSnack = req.body;
if (!newSnack.name || !newSnack.price) {
res.status(400).send(‘Snack not found!’);
} else {
newSnack.id = nextSnackId++;
vendingMachine.push(newSnack);
res.send(newSnack);
}
});

app.get(’/snacks/:snackId’, (req, res, next) => {
res.send(vendingMachine[req.snackIndex]);
});

app.put(’/snacks/:snackId’, (req, res, next) => {
vendingMachine[req.snackIndex] = req.body;
res.send(vendingMachine[req.snackIndex]);
});

app.delete(’/snacks/:snackId’, (req, res, next) => {
vendingMachine.splice(req.snackIndex, 1);
res.status(204).send();
});

app.listen(PORT, () => {
console.log(Server is listening on port ${PORT});
});

I mean, seriously. If you will only accept a specific configuration, a least spare me the two-faced questions. I just don’t care anymore, I will ask for the solution, but felt like I need to report this. Or I may be overreacting, it is quite possible with my current frustration level. What is wrong with my code?