2014-02-11 25 views
8

我有一個鐵路由器路由,我希望通過HTTP POST請求接收緯度/經度數據。如何從流星訪問HTTP POST數據?

這是我的嘗試:

Router.map(function() { 
    this.route('serverFile', { 
    path: '/receive/', 
    where: 'server', 

    action: function() { 
     var filename = this.params.filename; 
     resp = {'lat' : this.params.lat, 
       'lon' : this.params.lon}; 
     this.response.writeHead(200, {'Content-Type': 'application/json; charset=utf-8'}); 
     this.response.end(JSON.stringify(resp)); 
    } 
    }); 
}); 

但隨着查詢服務器:

curl --data "lat=12&lon=14" http://127.0.0.1:3000/receive 

返回{}。可能params不包含發佈信息嗎?我試圖檢查對象和請求,但我找不到它。

回答

14

iron-router中的connect framework使用bodyParser中間件來解析正文中發送的數據。 bodyParser使這些數據在request.body對象中可用。

對我來說,以下工作:

Router.map(function() { 
    this.route('serverFile', { 
    path: '/receive/', 
    where: 'server', 

    action: function() { 
     var filename = this.params.filename; 
     resp = {'lat' : this.request.body.lat, 
       'lon' : this.request.body.lon}; 
     this.response.writeHead(200, {'Content-Type': 
            'application/json; charset=utf-8'}); 
     this.response.end(JSON.stringify(resp)); 
    } 
    }); 
}); 

這給了我:

> curl --data "lat=12&lon=14" http://127.0.0.1:3000/receive 
{"lat":"12","lon":"14"} 

而且在這裏看到: http://www.senchalabs.org/connect/bodyParser.html

+0

** ** request.body就是我一直在尋找.. 謝謝! – gozzilli