简体   繁体   English

如何使用Meteor的WebApp访问HTTP POST正文(表单数据)? 还是其他?

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

I already tried server-routing with Iron Router, but that doesn't work. 我已经尝试使用Iron Router进行服务器路由,但这不起作用。 Then I discovered WebApp, which seems like it's supposed to handle this. 然后,我发现了WebApp,似乎应该可以解决这个问题。

But when I inspect the req object: 但是当我检查req对象时:

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

I don't see any of my POST form-data. 我看不到任何POST表单数据。 There's no body property, and I don't see the data itself anywhere under any other property. 没有body属性,在其他任何属性下,我都看不到数据本身。

How can I access this data? 我该如何访问这些数据? I'm going crazy trying to figure out something I thought would be relatively simple... 我疯了试图找出我认为相对简单的东西...

Yeah, I had the same issue. 是的,我有同样的问题。 With connect, the post body isn't automatically in the request object. 使用connect时,帖子主体不会自动出现在request对象中。 You can use middleware like this https://stackoverflow.com/a/24122700/5203563 or get it yourself like this: 您可以使用像这样的中间件https://stackoverflow.com/a/24122700/5203563或自己获得它,如下所示:

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

You will need to user fibers for the example to work, or I usually use picker you could add it meteor add meteorhacks:picker it's wrapper on top of webApp https://atmospherejs.com/meteorhacks/picker 您需要使用用户纤维才能使该示例正常工作,或者我通常使用选择器,您可以将其添加到meteor add meteorhacks:picker它是webApp https://atmospherejs.com/meteorhacks/picker的包装器

and your example should work this way: 您的示例应以这种方式工作:

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

here is an awesome article showing you the different options ( webApp with fibers as well) http://meteorpedia.com/read/REST_API 这是一篇很棒的文章,向您展示了不同的选择(还有带纤维的webApphttp://meteorpedia.com/read/REST_API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在angularjs中使用$ http.post()在post方法中使用enctype =“ multipart / form-data”发送表单数据 - how can i send the form data with enctype=“multipart/form-data” in the post method using $http.post() in angularjs 如何在流星模板中访问父母的数据属性? - How Do I Access a Parent's Data Attribute in Meteor Template? node.js,express,如何在post请求中从body form-data中获取数据 - node.js, express, how to get data from body form-data in post request 如何在没有 jQuery 的情况下使用 $http POST urlencoded 表单数据? - How do I POST urlencoded form data with $http without jQuery? 如何将文件发布到多部分/表单数据? - How to post files to a multipart/form-data? 如何使用 multipart/form-data 发送请求? - how do I send a request using multipart/form-data? 如何将表单数据发送到本地 MYSQL 数据库? - How do I send form-data to a local MYSQL database? 使用jQuery使用multipart / form-data进行HTTP POST调用? - Making an HTTP POST call with multipart/form-data using jQuery? 如何将参数传递给 http 请求,该请求以编程方式将 POST 有效负载作为表单数据 - how to pass parameters to an http request that call which takes the POST payload as form-data programmatically 如何在不带Ajax的流星中提交带有post方法的表单? - How do I submit a form with a post method in meteor without ajax?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM