【问题标题】:Meteor HTTP.POST call on same machine (for testing)Meteor HTTP.POST 在同一台机器上调用(用于测试)
【发布时间】:2018-05-14 02:16:36
【问题描述】:

我创建了一个服务器端路由(使用 Iron-router)。代码如下:

Router.route( "/apiCall/:username", function(){
var id = this.params.username;
},{ where: "server" } )

.post( function(req, res) {
// If a POST request is made, create the user's profile.
//check for legit request
console.log('post detected')
var userId = Meteor.users.findOne({username : id})._id;

})
.delete( function() {
// If a DELETE request is made, delete the user's profile.
});

这个应用程序在我本地的 3000 端口上运行。现在我创建了另一个在端口 5000 上运行的虚拟应用程序。从虚拟应用程序开始,我正在触发 http.post 请求,然后在 3000 端口上的应用程序上监听它。我使用以下代码通过虚拟应用程序触发 http.post 请求:

apiTest : function(){
    console.log('apiTest called')
     HTTP.post("http://192.168.1.5:3000/apiCall/testUser", {
      data: [
                {
                    "name" : "test"
                }
            ]
    }, function (err, res) {
        if(!err)
            console.log("succesfully posted"); // 4
        else
            console.log('err',err)
    });
    return true;
}

但我在回调中收到以下错误:

 err { [Error: socket hang up] code: 'ECONNRESET' }

无法弄清楚这里有什么问题。 服务端路由调用成功,但是没有进入 .post() 方法。 使用流星 1.6 版 192.168.1.5是我的ip地址

【问题讨论】:

    标签: http meteor connection iron-router


    【解决方案1】:

    好的,如果我使用Router.map函数,问题就解决了。

    Router.map(function () {
    this.route("apiRoute", {path: "/apiCall/:username",
    where: "server",
    action: function(){
      // console.log('------------------------------');
      // console.log('apiRoute');
      // console.log((this.params));
      // console.log(this.request.body);
      var id = this.params.username;
    
      this.response.writeHead(200, {
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': '*'
      });
    
      if (this.request.method == 'POST') {
        // console.log('POST');
        var user = Meteor.users.findOne({username : id});
        // console.log(user)
        if(!user){
          return 'no user found'
        }
        else{
          var userId = user._id;
        } 
    
      }
     });
     });
    

    【讨论】:

      【解决方案2】:

      内容类型似乎未设置为application/json。所以你应该这样做......

      Setting the "Content-Type" header in HTTP.call on client side in Meteor

      【讨论】:

      • 我有我的 http.post 方法以及我在服务器端的路由。我认为我们不需要在那里设置内容类型,对吗?如果我错了,请纠正我
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      • 2017-08-07
      • 2023-04-01
      • 2011-01-30
      • 2015-12-11
      相关资源
      最近更新 更多