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

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

问题描述

我有一个 Iron-router 路由,我想通过它通过 HTTP POST 请求接收 lat/lng 数据.

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

但是查询服务器:

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

返回{}.

也许 params 不包含发布数据?我试图检查对象和请求,但找不到.

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

推荐答案

iron-router 中的 connect framework 使用 bodyParser中间件来解析正文中发送的数据.bodyParser 使该数据在 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"}

另见此处:http://www.senchalabs.org/connect/bodyParser.html

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

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