2017-11-30 19 views
1

철분 라우터를 사용하여 서버 측 경로를 만들었습니다. 코드는 다음과 같습니다 :동일한 시스템에서 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에서 실행중인 다른 더미 응용 프로그램을 만들었습니다. 더미 응용 프로그램 인 Frrom에서 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 주소입니다.

답변

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