javascript - Sending data from node.js back to client? -
i sending post request node.js client .in handler of request making http post request(in node.js) data,which gives me json data in response ,then in turn data making http post request(in node.js) gives me set of data in response . want return response data handler function ,the set of data receive response can send client .how can achieve this.
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) }
i have send data have received in "returnfunc()" client in request handler of "/test".
simply pass through reply function callback functions
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); }); }
Comments
Post a Comment