我如何使用 Meteor 的 WebApp 访问 HTTP POST 正文(表单数据)?或者别的什么?

How do I access HTTP POST body (form-data) with Meteor's WebApp? Or anything else?

我已经尝试过使用 Iron Router 进行服务器路由,但这不起作用。然后我发现了 WebApp,它似乎应该可以处理这个问题。

但是当我检查请求对象时:

WebApp.connectHandlers.use("/api/add", function( req, res, next ) {
    console.log( req );
    res.writeHead(200);
    res.end("Hello world from: " + Meteor.release);
});

我没有看到任何 POST 表单数据。没有正文 属性,而且我在任何其他 属性.

下的任何地方都看不到数据本身

我如何访问这些数据?我快要发疯了,试图找出一些我认为相对简单的东西......

您将需要使用 fibers 才能使示例工作,或者我通常使用选择器,您可以添加它 meteor add meteorhacks:picker 它是 webApp https://atmospherejs.com/meteorhacks/picker [=16= 之上的包装器]

您的示例应该这样工作:

Picker.route('/api/add', function(params, req, res, next) {
    console.log( req );
    res.writeHead(200);
    res.end("Hello world from: " + Meteor.release);
});

这是一篇很棒的文章,向您展示了不同的选择(webApp 以及纤维)http://meteorpedia.com/read/REST_API

是的,我遇到了同样的问题。使用连接时,post 主体不会自动出现在请求对象中。您可以像这样使用中间件 或像这样自己获取:

WebApp.connectHandlers.use("/api/add", function( req, res, next ) {

  var body = "";
  req.on('data', Meteor.bindEnvironment(function (data) {
    body += data;
  }));

  req.on('end', Meteor.bindEnvironment(function () {
    console.log(body);
    res.writeHead(200);
    res.end("Hello world from: " + Meteor.release);
  }));
});