2012-04-20 4 views
1

여기에있는 것이 있습니다 : 서버에 데이터를 보내는 클라이언트가 있습니다. 이 서버는 외부 A.P.I에 문의해야합니다. 그 응답을 클라이언트에게 되돌려 보냅니다. 나는 일단 서버가 클라이언트 데이터를 얻었 으면 외부와 연락 할 수있는 방법과 장소를 파악할 수 없다. 이 같은ExpressJS - 외부 API에 문의

I의 클라이언트 데이터의 경로 :

app.post('/getAutoComplete', routes.read); 

routes.read이 req.body 내에서 데이터를 검색합니다.

var http = require('http'), options = { 
     host : "192.168.1.38", 
     port : 8080, 
     path : "/myURL", 
     method : 'POST' 
}; 

var webservice_data = ""; 

var webservice_request = http.request(options, function(webservice_response) 
{ 
    webservice_response.on('error', function(e){ console.log(e.message); }); 
    webservice_response.on('data', function(chunk){ webservice_data += chunk;}); 
    webservice_response.on('end', function(){res.send(webservice_data);}); 
}); 

webservice_request.write(req.body); 
webservice_request.end(); 

문제는 내가 app.post 같은 기본 expressJS 방법을 사용하고 싶지만 방법 때문에 내가 모르는 것입니다 : (고속 프레임 워크없이) 내 nodejs 버전으로, 나는 다음 API를 이런 식으로 요청 :

  1. 익스프레스 (응용 프로그램) 객체는 여기 내가 app.post
과 POST 데이터를 전송하는 방법을 모른다
  • (그러나 경로 파일의 app.js에 선언)하지

    제안 사항 ?

  • +0

    이것은 Express가 작동하는 방식이 아닙니다. "app.post'와 같은"expressJS 네이티브 메소드는 _receiving_ HTTP 요청을 보내고 보내지 않습니다. –

    +0

    그러면 외부 A.P.I.에 연락하려면 어떻게해야합니까? expressJS를 사용합니까? 내가 지금하고있는 것과 같은 방식으로? – Simon

    답변

    0

    routes.read은 기능입니다. 당신은 추가 매개 변수와 함께 호출 할 수 있습니다, 그래서 예를 들어

    app.post('/getAutoComplete', function(req,res) { 
        var q = req.query.q; // or whatever data you need 
        routes.read(q, function(err, response) { 
         if (err) throw err; 
         return res.json(response); 
        }); 
    }); 
    

    이제 routes.read 기능은 쿼리와 첫 번째 매개 변수를 사용하도록하고 원격 API의 응답을 수집 한 때 같은 오류와 함께 두 번째 매개 변수를 호출 첫 번째 매개 변수와 두 번째 매개 변수로 응답.

    업데이트이 답변은 이미 답변으로 포착하고있다,하지만 난 routes.read의 예를 보여 주었다 경우도 많은 도움이 될 것입니다 :

    routes.read = function(q, cb) { 
        // pretend we calculate the result 
        var result = q * 10; 
        if (result > 100) { 
         // call the callback with error set 
         return cb("q value too high"); 
        } 
        // all is well, use setTimeout to demonstrate 
        // an asynchronous return 
        setTimeout(function() { cb(null, result) }, 2000); 
    }; 
    
    2
    app.post('/getAutoComplete', routes.read); 
    // assuming routes.read lookes something like this 
    routes.read = function read(req, res) { 
        var http = require('http'), options = { 
          host : "192.168.1.38", 
          port : 8080, 
          path : "/myURL", 
          method : 'POST' 
        }; 
    
        var webservice_data = ""; 
    
        var webservice_request = http.request(options, function(webservice_response) 
        { 
         webservice_response.on('error', function(e){ console.log(e.message); }); 
         webservice_response.on('data', function(chunk){ webservice_data += chunk;}); 
         webservice_response.on('end', function(){res.send(webservice_data);}); 
        }); 
    
        webservice_request.write(req.body); 
        webservice_request.end(); 
    }; 
    

    또한 https://github.com/mikeal/request 체크 아웃 노드에서 웹 요청을 수행하기위한 사실상의 모듈입니다.