2

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.

1
  • I am using hapi.js framework on top of node.js Commented Nov 1, 2015 at 13:32

1 Answer 1

2

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);
    });
}
Sign up to request clarification or add additional context in comments.

2 Comments

but Still the data is not coming back to the client .The request is successful , and yes i applied your method passing the callback functions.I am able to see the data at the server level.But the client level the data at the handler of ajax request its giving me blank .
My bad , I am sorry , My mistake . It worked . Thank you so much . i appreciate this .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.