2017-11-30 7 views
1

私はサーバー側のルートを作成しました(iron-routerを使用しています)。コードは次のとおりです:同じマシンでMeteor HTTP.POSTを呼び出す(テスト用)

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 addrです

答えて

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

    } 
}); 
}); 
0

コンテンツタイプがapplication/jsonに設定されていないようです。だからあなたはそれを行う必要があります...わかりまし

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

+0

私は自分のhttp.postメソッドとサーバー側の私のルートを持っています。私たちはコンテンツタイプを設定する必要があるとは思わないのですか?私が間違っていれば私を修正してください – user3807691

関連する問題