[英]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 这是一篇很棒的文章,向您展示了不同的选择(还有带纤维的webApp
) http://meteorpedia.com/read/REST_API
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.