2015-11-01 141 views
1

我正在向客戶端發送一個POST請求給node.js。在請求的處理程序中,我使用數據發出了一個http Post Request(在node.js中),這給了我一個響應中的json數據,然後依次該數據我正在做另一個HTTP POST請求(在node.js中),它給了我一組響應的數據。現在我想把這個響應數據返回給處理函數,這樣,作爲響應接收到的數據集可以發送回客戶端。我可以如何實現這一點。將數據從node.js發回客戶端?

server.route({ 
path:"/test", 
method:"POST", 
handler:function(request,reply){ 
    var load=request.payload; 
    UserAuth(load); 
    return reply({status:"Ok"}); 
    } 

}); 
function UserAuth(newdata){ 

request({ 
    har:{ 
    url:"URL", 
    method:"POST", 
    headers:[ 
     { 
     name:'content-Type', 
     value:"application/x-www-form-urlencoded;charset=utf-8" 
    } 
    ], 
    postData:{ 
     mimeType: 'application/x-www-form-urlencoded', 
     params:[ 
      { 
       name:"username", 
       value:UserDetails["username"] 
      }, 
      { 
       name:"password", 
       value:UserDetails["password"] 

      }, 
      { 
       name:"output_mode", 
       value:UserDetails["output_mode"] 

      } 
     ] 
    } 
} 
},function(error,response,body){ 

    var obj = JSON.parse(body); 
    if(obj.sessionKey != null || obj.sessionKey!=""){ 
     PostDataToS(newdata,obj.sessionKey); 

    }else{ 

     console.log(error); 
    } 





}); 

} 
function PostDataToS(data,sessionkey){ 

request({ 
    har:{ 
    url:SEARCH_URL, 
    method:"POST", 
    headers:[ 
     { 
     name:'content-Type', 
     value:"application/x-www-form-urlencoded;charset=utf-8" 
    }, 
     { 
      name:"Authorization", 
      value:sessionkey 
    } 
    ], 
    postData:{ 
     mimeType: 'application/x-www-form-urlencoded', 
     params:[ 
      { 
       name:"search", 
       value:data["search"] 
      }, 
      { 
       name:"preview", 
       value:"false" 

      }, 
      { 
       name:"output_mode", 
       value:"json" 

      }, 
      { 
       name:"exec_mode", 
       value:"oneshot" 

      } 

     ] 
    } 
} 
},function(error,response,body){ 

    obj2 = JSON.parse(body); 
    var secondLayer=obj2.results; 
    returnfunc(secondLayer); 



}); 

} 

function returnfunc(data){ 
console.log("inside return func"); 
console.log(data) 

} 

我必須將「returnfunc()」中收到的數據發送回「/ test」請求處理程序中的客戶端。

+0

我使用hapi.js框架上的Node.js –

回答

1

只需通過回覆功能傳遞給你的回調函數

server.route({ 
    path:"/test", 
    method:"POST", 
    handler:function(request,reply){ 
     var load=request.payload; 
     UserAuth(load); 
    } 
}); 
function UserAuth(newdata, reply){ 
    request({...},function(error,response,body){ 
     var obj = JSON.parse(body); 
     if(obj.sessionKey != null || obj.sessionKey!=""){ 
      PostDataToS(newdata,obj.sessionKey, reply); 
     } else { 
      console.log(error); 
     } 
    }); 
} 
function PostDataToS(data, sessionkey, reply){ 
    request({...},function(error,response,body){ 
     obj2 = JSON.parse(body); 
     var secondLayer=obj2.results; 
     reply.continue(obj2.results); 
    }); 
} 
+0

的上端,但還是數據不回來的客戶.The請求成功,是的,我應用你的方法傳遞迴調函數。我能夠在服務器級別看到數據。但客戶端級別的數據在Ajax的處理程序請求它給我空白。 –

+0

我的壞,我很抱歉,我的錯誤。有效 。非常感謝 。我很欣賞這個 。 –

相關問題