2017-10-05 93 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

非常感謝!你擊中了要害。我陷入了雜草,看不到退路。非常感謝您的幫助! –