2017-10-05 48 views
0

所有,Node.js的http.request结果返回可变

我试图找出如何从https.request在node.js的代码将结果传递出一个变量。我有一个https.request设置,可以正确地将正确的信息传递给SOAP API并获取正确的响应。我的最终目标是将https.request的输出转换为我可以使用Express调用的变量。

这是我的代码块。

HTML:

   <div class="row"> 
       <div class="col-md-12" class="pull-left"> 
        <p> TEST </p> 
        <p>{{soapreply}}</p> 
       </div> 

JS:

app.post('/cucmmapper/submit', function (req, res) { 
// FORM - DATA COLLECTION 
var cucmpub = req.body.cucmpub; 
var cucmversion = req.body.cucmversion; 
var username = req.body.username; 
var password = req.body.password; 
var authentication = username + ":" + password; 
var soapreplyx = ''; 

// SOAP - BUILD CALL 
var https = require("https"); 
var headers = { 
    'SoapAction': 'CUCM:DB ver=' + cucmversion + ' listCss', 
    'Authorization': 'Basic ' + new Buffer(authentication).toString('base64'), 
    'Content-Type': 'text/xml; charset=utf-8' 
}; 

// SOAP - AXL CALL 
var soapBody = new Buffer('<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://www.cisco.com/AXL/API/11.5">' + 
    '<soapenv:Header/>' + 
    '<soapenv:Body>' + 
    '<ns:listCss sequence="?">' + 
    '<searchCriteria>' + 
    '<name>%</name>' + 
    '</searchCriteria>' + 
    '<returnedTags uuid="?">' + 
    '<name>?</name>' + 
    '<description>?</description>' + 
    '<clause>?</clause>' + 
    '</returnedTags>' + 
    '</ns:listCss>' + 
    '</soapenv:Body>' + 
    '</soapenv:Envelope>'); 

// SOAP - OPTIONS 
var options = { 
    host: cucmpub, // IP ADDRESS OF CUCM PUBLISHER 
    port: 8443, // DEFAULT CISCO SSL PORT 
    path: '/axl/', // AXL URL 
    method: 'POST', // AXL REQUIREMENT OF POST 
    headers: headers, // HEADER VAR 
    rejectUnauthorized: false // REQUIRED TO ACCEPT SELF-SIGNED CERTS 
}; 

// SOAP - Doesn't seem to need this line, but it might be useful anyway for pooling? 
options.agent = new https.Agent(options); 

// SOAP - OPEN SESSION 
var req = https.request(options, function (res) { 
    res.setEncoding('utf8'); 
    res.on('data', function (d) { 
    soapreplyx = d; 
    console.log("Got Data: " + d); 
    }); 
}); 

// SOAP - SEND AXL CALL 
req.write(soapBody); 
res.render('cucmmapper-results.html'), { 
    'title': 'CUCM 2.1', 
    'soapreply': soapreplyx 
}; 
req.end(); 
req.on('error', function (e) { 
    console.error(e); 
}); 

}); }

行“console.log(”Got Data:“+ d)”正在从API获取正确的预期回复,但是,我无法弄清楚如何将该数据存入我的变量“soapreplyx “在Express中改变为”soapreply“。

非常感谢您的帮助!

回答

0

在您致电res.render()之前,您并不等待您的请求,因此soapreplyx的值始终为其初始值''。要解决此问题,请在传递给您的https.request()回调的响应对象上添加一个'end'事件侦听器。

您不是将响应的块附加到您的soapreplyx变量上,而是将它的值重新分配给每个连续的块。

let soapRequest = https.request(options, soapResponse => { 
    soapResponse.on('data', chunk => { 
    soapreplyx += chunk 
    }) 

    soapResponse.on('end',() => { 
    return res.render('cucmmapper-results.html', { 
     title: 'CUCM 2.1', 
     soapreply: soapreplyx 
    }) 
    }) 
}) 

soapRequest.write(soapBody) 
soapRequest.end() 
+0

非常感谢!你击中了要害。我陷入了杂草,看不到退路。非常感谢您的帮助! –