2013-12-08 4 views
0

익스프레스를 사용하여 JSON을 노드에서 직접 다른 서버에 게시 할 수있게하려고합니다. 기본적으로 다른 api를 호출 할 때 API 키를 노출하고 싶지 않지만 여전히 서비스를 호출 할 수 있습니다. 내가 원하는 클라이언트에서 https://github.com/GetResponse/DevZone/blob/master/API/examples/javascript_synopsis.html익스프레스에서 외부 서버로 JSON 게시

JS 구현할 :

 var api_key = 'ENTER_YOUR_API_KEY_HERE'; 

     // API 2.x URL 
     var api_url = 'http://api2.getresponse.com'; 

     function add_contact() { 

      var campaigns = {}; 

      // find campaign named 'test' 
      $.ajax({ 
       url  : api_url, 
       data : JSON.stringify({ 
        'jsonrpc' : '2.0', 
        'method' : 'get_campaigns', 
        'params' : [ 
         api_key, 
         { 
          // find by name literally 
          'name' : { 'EQUALS' : 'test' } 
         } 
        ], 
        'id'  : 1 
       }), 
       type  : 'POST', 
       contentType : 'application/json', 
       dataType : 'JSON', 
       crossDomain : true, 
       async  : false, 
       success  : function(response) {       
        // uncomment following line to preview Response 
        // alert(JSON.stringify(response)); 

        campaigns = response.result; 
       } 
      }); 

      // because there can be only (too much HIGHLANDER movie) one campaign of this name 
      // first key is the CAMPAIGN_ID required by next method 
      // (this ID is constant and should be cached for future use) 
      var CAMPAIGN_ID; 
      for(var key in campaigns) { 
       CAMPAIGN_ID = key; 
       break; 
      } 

      $.ajax({ 
       url  : api_url, 
       data : JSON.stringify({ 
        'jsonrpc' : '2.0', 
        'method' : 'add_contact', 
        'params' : [ 
         api_key, 
         { 
          // identifier of 'test' campaign 
          'campaign' : CAMPAIGN_ID, 

          // basic info 
          'name'  : 'Test', 
          'email'  : '[email protected]', 

          // custom fields 
          'customs' : [ 
           { 
            'name'  : 'likes_to_drink', 
            'content' : 'tea' 
           }, 
           { 
            'name'  : 'likes_to_eat', 
            'content' : 'steak' 
           } 
          ] 
         } 
        ], 
        'id'  : 2 
       }), 
       type  : 'POST', 
       contentType : 'application/json', 
       dataType : 'JSON', 
       crossDomain : true, 
       async  : false, 
       success  : function(response) 
       {       
        // uncomment following line to preview Response 
        // alert(JSON.stringify(response)); 

        alert('Contact added'); 
       } 
      }); 

     } 

답변

3

노드 서버가 클라이언트 요청에 대해 타사 서버의 프록시 역할을 할 수 있다고 생각합니다.

서버가 API 호출에 필요한 모든 입력 매개 변수 (예 : add_contact)를 수집 할 수 있습니다. 타사 서버에 액세스하기위한 올바른 자격 증명이있는 노드 서버가 api 호출을하고 클라이언트에 수신 된 응답을 전달합니다.

노드에 내장 된 http 라이브러리를 사용하거나 the request module (더 편리)을 사용하여 이러한 호출을 할 수 있습니다.

기본적으로 필요한 외부 api에 대한 래퍼를 만들어야합니다. 그러면 모든 설정이 완료됩니다.

도움이되기를 바랍니다.

+0

감사합니다.이 기능이 작동하는지 여부를 확인할 기회가 없었지만, 필자가 설명한 것과 정확히 똑같이하려고합니다. 요청 모듈이나 http://nodejs.org/api/http.html#http_http_request_options_callback을 사용해야합니까? 귀하가 지적한 요청 모듈의 이점은 무엇입니까? – BRogers

+0

요청 모듈을 사용하는 것이 더 편리합니다. 사용을 권장합니다. – vmx

+0

대단히 고마워! – BRogers