how do I access a named parameter in a function?

Problem

I'm developing a REST web service using Node JS to work in conjunction with Backbone JS. One of the REST methods is GET /users/id/:id, where :id is the id number of a user. This method will return the details of the user from a database.

What I don't understand is how can I pass the :id parameter from the url to the response handler.

I've defined the response handler in app.js like this:

app.get('users/id/:id',user.fetch(db));

and this is the user.fetch function

exports.fetch = function(db){
    return function(req,res){

        var id = ;//how do I get the Id from the request?
        console.log("id: "+id);
        if(id !== null){
            peopleDb = db.get('people');
            peopleDb.find({"_id":id},function(e,docs){
                if(e){
                console.log(e);
                }else
                {
                    res.setHeader('Content-Type','application/json');
                    res.setHeader('Access-Control-Allow-Origin','*');
                    res.setHeader('Access-Control-Allow-Methods','GET,PUT,POST,DELETE');
                    res.writeHead(200);
                    res.end(JSON.stringify(docs));
                }
                });
        }
    }
}
Problem courtesy of: W.K.S

Solution

return function(req,res)
   {
      var id = req.params.id;
      console.log("id: "+id);
      // ...  

this should works as expected. You can read more here

Solution courtesy of: Ilya

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.