最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin5浏览0评论

I already tried server-routing with Iron Router, but that doesn't work. Then I discovered WebApp, which seems like it's supposed to handle this.

But when I inspect the req object:

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. There's no body property, and I don't see the data itself anywhere under any other property.

How can I access this data? I'm going crazy trying to figure out something I thought would be relatively simple...

I already tried server-routing with Iron Router, but that doesn't work. Then I discovered WebApp, which seems like it's supposed to handle this.

But when I inspect the req object:

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. There's no body property, and I don't see the data itself anywhere under any other property.

How can I access this data? I'm going crazy trying to figure out something I thought would be relatively simple...

Share Improve this question asked Sep 21, 2015 at 22:26 m52gom52go 3431 gold badge3 silver badges14 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

Yeah, I had the same issue. With connect, the post body isn't automatically in the request object. You can use middleware like this https://stackoverflow./a/24122700/5203563 or get it yourself like this:

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);
  }));
});
发布评论

评论列表(0)

  1. 暂无评论