programing

Meteor를 이용한 API 호출 방법

css3 2023. 3. 21. 22:23

Meteor를 이용한 API 호출 방법

여기 twitter API가 있습니다.

http://search.twitter.com/search.atom?q=perkytweets

이 API를 호출하거나 Meteor를 사용하여 링크하는 방법에 대해 힌트를 줄 수 있는 사람 있나요?

업데이트:

여기 내가 시도했지만 아무런 반응이 보이지 않는 코드가 있다.

if (Meteor.isClient) {
    Template.hello.greeting = function () {
        return "Welcome to HelloWorld";
    };

    Template.hello.events({
        'click input' : function () {
            checkTwitter();
        }
    });

    Meteor.methods({checkTwitter: function () {
        this.unblock();
        var result = Meteor.http.call("GET", "http://search.twitter.com/search.atom?q=perkytweets");
        alert(result.statusCode);
    }});
}

if (Meteor.isServer) {
    Meteor.startup(function () {
    });
}

CheckTwitter Meteor를 정의하고 있습니다.method in client-scope blocks.클라이언트에서 크로스 도메인을 호출할 수 없기 때문에(jsonp를 사용하지 않는 한), 이 블록을Meteor.isServer차단합니다.

참고로 설명서에 따르면 클라이언트 측에서는Meteor.methodcheckTwitter 함수는 서버측 메서드의 stub에 불과합니다.서버측과 클라이언트측의 상세한 것에 대하여는, 문서를 참조해 주세요.Meteor.methods힘을 합치다

다음으로 http 콜의 동작 예를 나타냅니다.

if (Meteor.isServer) {
    Meteor.methods({
        checkTwitter: function () {
            this.unblock();
            return Meteor.http.call("GET", "http://search.twitter.com/search.json?q=perkytweets");
        }
    });
}

//invoke the server method
if (Meteor.isClient) {
    Meteor.call("checkTwitter", function(error, results) {
        console.log(results.content); //results.data should be a JSON object
    });
}

이것은 기본적인 것처럼 보일 수 있지만, Meteor 프로젝트에서는 HTTP 패키지가 기본적으로 제공되지 않기 때문에 설치를 개별적으로 해야 합니다.

명령줄에서 다음 중 하나를 수행합니다.

  1. Just Meteor:
    유성 추가 http

  2. 운석:
    mrt add http

Meteor HTTP 문서

클라이언트의 Meteor.http.get은 비동기이므로 콜백 함수를 제공해야 합니다.

Meteor.http.call("GET",url,function(error,result){
     console.log(result.statusCode);
});

사용하다Meteor.http.get. 문서별:

Meteor.http.get(url, [options], [asyncCallback]) Anywhere
Send an HTTP GET request. Equivalent to Meteor.http.call("GET", ...).

이 문서에는 실제로 트위터 사용 예가 포함되어 있기 때문에 시작할 수 있을 것입니다.

http.get에 콜백하면 비동기 콜이 되기 때문에 정의되지 않은 클라이언트의 리턴에 대한 나의 솔루션은

var result = HTTP.get(iurl), result.data.response를 반환합니다.

HTTP.get에 콜을 반환하지 않았기 때문에 응답을 받을 때까지 대기했습니다.도움이 되었으면 좋겠다

언급URL : https://stackoverflow.com/questions/14320610/how-to-make-an-api-call-using-meteor