简体   繁体   English

将数据从 Node.js 发送回客户端?

[英]Sending data from Node.js back to client?

I am sending a POST request to Node.js from client.我正在从客户端向 Node.js 发送 POST 请求。 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.在请求的处理程序中,我正在使用数据发出 HTTP POST 请求(在 Node.js 中),它给我一个 JSON 数据作为响应,然后我又使用该数据发出另一个 HTTP POST 请求(在 Node.js 中)这给了我一组数据作为回应。 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 .我必须将我在returnfunc()收到的数据发送回/test的请求处理程序中的客户端。

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);
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM