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 trial

JavaScript

Lucas Santos
Lucas Santos
19,315 Points

Don'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!

Andrew Chalkley

2 Answers

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Good 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.

Lucas Santos
Lucas Santos
19,315 Points

Got it, thanks a ton Andrew really appreciate your time! You're the man!

I'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
Lucas Santos
19,315 Points

Hey 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.