我想知道在node.js中使用SOAP XML web服务的最佳方式是什么。
谢谢!
发布于 2017-01-25 04:02:48
我设法使用soap,wsdl和Node.js你需要用npm install soap
安装soap
创建一个名为server.js
的节点服务器,它将定义要由远程客户端使用的soap服务。此soap服务根据体重(Kg)和身高(M)计算身体质量指数。
const soap = require('soap');
const express = require('express');
const app = express();
/**
* this is remote service defined in this file, that can be accessed by clients, who will supply args
* response is returned to the calling client
* our service calculates bmi by dividing weight in kilograms by square of height in metres
*/
const service = {
BMI_Service: {
BMI_Port: {
calculateBMI(args) {
//console.log(Date().getFullYear())
const year = new Date().getFullYear();
const n = args.weight / (args.height * args.height);
console.log(n);
return { bmi: n };
}
}
}
};
// xml data is extracted from wsdl file created
const xml = require('fs').readFileSync('./bmicalculator.wsdl', 'utf8');
//create an express server and pass it to a soap server
const server = app.listen(3030, function() {
const host = '127.0.0.1';
const port = server.address().port;
});
soap.listen(server, '/bmicalculator', service, xml);
接下来,创建将使用由server.js
定义的soap服务的client.js
文件。此文件将为soap服务提供参数,并使用SOAP的服务端口和端点调用url。
const express = require('express');
const soap = require('soap');
const url = 'http://localhost:3030/bmicalculator?wsdl';
const args = { weight: 65.7, height: 1.63 };
soap.createClient(url, function(err, client) {
if (err) console.error(err);
else {
client.calculateBMI(args, function(err, response) {
if (err) console.error(err);
else {
console.log(response);
res.send(response);
}
});
}
});
wsdl文件是基于xml的数据交换协议,用于定义如何访问远程web服务。将wsdl文件命名为bmicalculator.wsdl
。
<definitions name="HelloService" targetNamespace="http://www.examples.com/wsdl/HelloService.wsdl"
xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.examples.com/wsdl/HelloService.wsdl"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<message name="getBMIRequest">
<part name="weight" type="xsd:float"/>
<part name="height" type="xsd:float"/>
</message>
<message name="getBMIResponse">
<part name="bmi" type="xsd:float"/>
</message>
<portType name="Hello_PortType">
<operation name="calculateBMI">
<input message="tns:getBMIRequest"/>
<output message="tns:getBMIResponse"/>
</operation>
</portType>
<binding name="Hello_Binding" type="tns:Hello_PortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="calculateBMI">
<soap:operation soapAction="calculateBMI"/>
<input>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:helloservice" use="encoded"/>
</input>
<output>
<soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="urn:examples:helloservice" use="encoded"/>
</output>
</operation>
</binding>
<service name="BMI_Service">
<documentation>WSDL File for HelloService</documentation>
<port binding="tns:Hello_Binding" name="BMI_Port">
<soap:address location="http://localhost:3030/bmicalculator/" />
</port>
</service>
</definitions>
希望能有所帮助
发布于 2016-05-21 02:57:26
我发现使用Node.js将原始XML发送到SOAP服务的最简单方法是使用Node.js http实现。它看起来像这样。
var http = require('http');
var http_options = {
hostname: 'localhost',
port: 80,
path: '/LocationOfSOAPServer/',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': xml.length
}
}
var req = http.request(http_options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.')
})
});
req.on('error', (e) => {
console.log(`problem with request: ${e.message}`);
});
// write data to request body
req.write(xml); // xml would have been set somewhere to a complete xml document in the form of a string
req.end();
您可以将xml变量定义为字符串形式的原始xml。
但是,如果您只想通过Node.js与SOAP服务交互并进行常规的SOAP调用,而不是发送原始的xml,那么可以使用Node.js库之一。我喜欢node-soap。
发布于 2015-04-18 00:11:20
我在10多个跟踪WebApis上成功地使用了"soap“包(https://www.npmjs.com/package/soap) (Tradetracker,Bbelboon,Affilinet,Webgains,...)。
问题通常来自这样一个事实,即程序员不会过多地调查远程API需要什么才能进行连接或身份验证。
例如,PHP自动从HTTP头重新发送cookie,但当使用'node‘包时,它必须显式设置(例如通过'soap-cookie’包)...
https://stackoverflow.com/questions/8655252
复制相似问题