Search code examples
javascriptnode.jspostclient

Sending data from Node.js back to client?


I am sending a POST request to Node.js from client. In the handler of the request I am making an HTTP POST request (in Node.js) with data, which gives me a JSON data in response, then in turn with that data I am making another HTTP POST request (in Node.js) which gives me a set of data in response. Now I want to return this response data to the handler function so that, the set of data which receive as response I can send back to client. How can I 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 to send data which I have received in returnfunc() back to the client in the request handler of /test.


Solution

  • Simply pass through the reply function to your 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);
        });
    }