FAQs on the exercise Route-Level app.use() - Multiple Paths
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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
Hi!
I think I’m confused. A couple exercises ago, we learned that paths in an app.use piece of middleware were specific to the path listed and not other child paths:
In the example above the console will print 'User has hit endpoint /sorcerer' , if someone visits our web page’s ‘/sorcerer’ endpoint. Since the method app.use() was used, it won’t matter if the user is performing a GET ,a POST , or any other kind of HTTP request. Since the path was given as an argument to app.use() , this middleware function will not execute if the user hits a different path (for instance: '/spells' or '/sorcerer/:sorcerer_id' ).
However, in the solution for this exercise, it requires me to create a line for app.use with the path array of [‘/beans/’, ‘/beans/:beanName’], but then expects me to refactor/remove code under the PUT route of app.put(‘/beans/:beanName/name’.
If app.use(‘/sorcerer’) doesn’t execute the middleware on app.get(‘sorcerer/:sorcerer_id), why does the app.use( [’/beans/', ‘/beans/:beanName’]) execute the middleware on app.put(‘beans/:beanName/name’ in this exercise?
I don’t know why Codecademy expects me to know how to write a new call for multiple routes before ever seeing one.
It is frustrating to see Codecademy expect learners to know things not yet taught. I find myself going to the solution before even getting past the first problem. As others mentioned before, this Express course isn’t very helpful.
I understand your frustration. This course has been vague at times. On this particular question it asks us to make an array with the provided routes as elements. If you can’t remember how arrays work I would suggest going back and reviewing Javascript Syntax Part II. Or just googling Javascript Array Documentation.
Make sure that it matches all routes for '/beans/' and '/beans/:beanName' using the array of routes syntax.
I understood from the lesson that I can write the few path as a array. I didn’t understand what benefit I got from this, I didn’t understand what “.on” does, I also see it for the first time, how JSON body parser works. I do guessing tasks, like Sudoku, to match the answer
Maybe the developers missed some skill requirements before taking this course?
I’m glad that I’m not the only one who thinks, this course lacks good explanations. This course is really hard to follow, the code is overcomplicated, so it gets even harder to understand what is happening.
I had a question about mentioning multiple paths in app.use(). In given example, we implement this:
[javascript]
// middleware to combine HTTP request body
app.use([‘/beans/’, ‘/beans/:beanName’], (req, res, next) => {
let bodyData = ‘’;
req.on(‘data’, (data) => {
bodyData += data;
});
req.on(‘end’, () => {
if (bodyData) {
req.body = JSON.parse(bodyData);
}
next();
});
})
[/javascript]
Why is there a need to mention ‘/beans/:beanName’ in path in addition to ‘/beans/’ ? I mean since app.use(‘/beans/’, ()); matches all paths with base ‘/beans/’, wouldn’t ‘/beans/:beanName’ also be considered under it? I am just confused about this one. Would really appreciate it if anyone could provide some insight here.
I think it’s alright, I am going through it for the second time and I’m able to understand it better. It takes time, but the material is comprehensive. The only way to understand better is to implement it on your own in projects.
Hello all. I had the same question and confusion as @jaime.savasta . I believe that Codecademy interpreted wrong and confused lots of people(including me) in step 6 in Route-Level app.use() - Single Path . However, as per the documentation:
A route will match any path that follows its path immediately with a “/ ”. For example: app.use('/apple', ...) will match “/apple”, “/apple/images”, “/apple/images/news”, and so on.
The lesson correctly said that with a path of ‘/sorcerer’, /sorcerer/:sorcerer_id won’t execute. That is because the path isn’t followed by /. If it had being followed by /, like /sorcerer/ then it would have been executed for /sorcerer/:sorcerrer_id.
app.use([‘/beans/’, ‘/beans/:beansName’]) executes app.put(‘/beans/:beanName/name’ because the first element in the array of the use middleware catches this path, much like a wildcard(the ‘/beans/’ element).
This course in general needs a good rewriting. It seems as if they don’t adhere to the official documentation and interpret it in an ambiguous manner.