如何从流星访问HTTP POST数据? [英] How do I access HTTP POST data from meteor?

查看:191
本文介绍了如何从流星访问HTTP POST数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想通过HTTP POST请求接收纬度/经度数据的铁路由器的路由。

I have an Iron-router route with which I would like to receive lat/lng data through an HTTP POST request.

这是我的尝试:

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

但随着查询服务器:

But querying the server with:

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

返回 {}

也许 PARAMS 不包含POST数据?我试图检查对象和要求,但我不能找到它。

Maybe params doesn't contain post data? I tried to inspect the object and the request but I can't find it.

推荐答案

href=\"http://www.senchalabs.org/connect/\">铁路由​​器中连接框架的 request.body 对象数据。

The connect framework within iron-router uses the bodyParser middleware to parse the data that is sent in the body. The bodyParser makes that data available in the request.body object.

对我来说,以下工作:

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"}

在此还看到:
<一href=\"http://www.senchalabs.org/connect/bodyParser.html\">http://www.senchalabs.org/connect/bodyParser.html

这篇关于如何从流星访问HTTP POST数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆