如何使用Meteor的WebApp访问HTTP POST主体(表单数据)?还是其他什么?

m52*_*2go 0 javascript node.js meteor iron-router

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

但是当我检查req对象时:

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

我没有看到任何POST表单数据.没有身体属性,我没有看到任何其他属性下的数据本身.

我该如何访问这些数据?我疯狂地想弄清楚我认为会比较简单的东西......

Ale*_*502 6

是的,我有同样的问题.使用connect时,帖子正文不会自动出现在请求对象中.您可以使用这样的中间件/sf/answers/1688589031/或者像这样自己动手:

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);
  }));
});
Run Code Online (Sandbox Code Playgroud)