【发布时间】:2015-11-01 13:20:06
【问题描述】:
我正在从客户端向 Node.js 发送 POST 请求。在请求的处理程序中,我正在使用数据发出 HTTP POST 请求(在 Node.js 中),这给了我一个 JSON 数据作为响应,然后我又用该数据发出另一个 HTTP POST 请求(在 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 的请求处理程序中的客户端。
【问题讨论】:
-
我在 node.js 之上使用 hapi.js 框架
标签: javascript node.js post client