2013-11-02 18 views
0

我正在開發一個REST Web服務,使用Node JS與Backbone JS一起工作。 其中一種REST方法是GET /users/id/:id,其中:id是用戶的id號。此方法將從數據庫中返回用戶的詳細信息。如何訪問函數中的命名參數?

我不明白的是如何將url參數傳遞給響應處理程序。

我定義在app.js的響應處理程序是這樣的:

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

,這是user.fetch功能

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

你想要什麼還不清楚,至少對我而言。 –

+0

非常感謝您的支持,我會盡量讓它更加詳細。 –

回答

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

這應該按預期工作。你可以閱讀更多here

相關問題