繁体   English   中英

Node.js http.request结果返回为变量

[英]Node.js http.request Results Back as Variable

所有,

我试图弄清楚如何将node.js代码中来自https.request的结果传递给变量。 我有一个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”。

非常感谢您提供的任何帮助!

在调用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()

暂无
暂无

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

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