Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialLucas Santos
19,315 PointsDon't understand the context Andrew used router.param
In the "Build a REST API with Express" course I saw Andrew write this piece of middleware.
router.param("qID", function(req,res,next,id){
Question.findById(id, function(err, doc){
if(err) return next(err);
if(!doc) {
err = new Error("Not Found");
err.status = 404;
return next(err);
}
req.question = doc;
return next();
});
});
I understand everything inside of the middleware, but I just do not understand this part
router.param("qID", function(req,res,next,id){
would someone please explain that part of the code and what the param is doing here and why pass in an id parameter as the last argument.
Thank you!
2 Answers
Andrew Chalkley
Treehouse Guest TeacherGood question.
The param
method is a special type of middleware that gets triggered when a param is in a route, id
is the variable that value of that param. At the moment this gets triggered if the qID
is present however, param
can also take an array of route parameters, for example ['userID', 'adminID', 'moderatorID']
maybe there's a reason why you'd have different names in your route parameters but the value is used in a similar way like, User.find(id, ...)
so instead of having a lot of branching logic like:
router.param(['userID', 'adminID', 'moderatorID'], function(req,res,next){
var userID;
if(req.params.userID) {
userID = req.params.userID;
} else if(req.params.adminID) {
userID = req.params.adminID;
} else {
userID = req.params.moderatorID;
}
User.find(userID, ...);
});
You can simply write:
router.param(['userID', 'adminID', 'moderatorID'], function(req,res,next,id){
User.find(id, ...);
});
For more info on the param method check out the documentation on the Express site.
Edward Ries
7,388 PointsI've not taken the course or worked with express but it appears that Question.findById returns a promise. Err will be populated if the Question service failed. This might happen if the remote api is offline or it doesn't have permission, etc.
The next thing the code does is to test to see if a doc which is the result is populated. If not then it returns a 404 file not found http error.
Finally, if there wasn't an error return and the doc is populated then it returns the doc.
I hope that helps.
Lucas Santos
19,315 PointsHey Edward I really appreciate your reply, but as mentioned in my question above I already understand everything that is happening inside of the middleware. The only part that I do not understand is . . .
router.param("qID", function(req,res,next,id){});
How does the router.param work in this situation is my question.
Again thanks for your time Edward I really appreciate it.
Lucas Santos
19,315 PointsLucas Santos
19,315 PointsGot it, thanks a ton Andrew really appreciate your time! You're the man!