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));
}
});
}
}
}