我尝试使用node.js使用node-soap制作soap服务器。我有像 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="SayHelloRequest">
<part name="firstName" type="xsd:string"/>
</message>
<message name="SayHelloResponse">
<part name="greeting" type="xsd:string"/>
</message>
<portType name="Hello_PortType">
<operation name="sayHello">
<input message="tns:SayHelloRequest"/>
<output message="tns:SayHelloResponse"/>
</operation>
</portType>
<binding name="Hello_Binding" type="tns:Hello_PortType">
<soap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="sayHello">
<soap:operation soapAction="sayHello"/>
<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="Hello_Service">
<documentation>WSDL File for HelloService</documentation>
<port binding="tns:Hello_Binding" name="Hello_Port">
<soap:address
location="http://localhost:8000/wsdl">
</port>
</service>
</definitions>
和我的代码
var http = require('http');
var soap = require('soap');
var helloService = {
Hello_Service: {
Hello_Port: {
SayHelloRequest: function(args) {
return {
firstName: args.name
};
}
}
}
}
var xml = require('fs').readFileSync('HelloService.wsdl', 'utf8'),
server = http.createServer(function(request,response) {
response.end("404: Not Found: "+request.url)
});
server.listen(8000);
soap.listen(server, '/wsdl', helloService, xml);
我把它们放在同一个目录中,但出现错误
/nodejs_ws_demo/node_modules/soap/lib/wsdl.js:937 抛出新的错误(p.getError()); ^错误:标记不匹配
我该如何解决它。
除了Vinayr的答案之外,将函数更改为
var helloService = {
Hello_Service: {
Hello_Port: {
sayHello: function(args) {
return {
greeting: "Hello!!!"
};
}
}
}
}
在代码中修复此问题(代码中的方法名称和输出参数错误)。
sayHello
是 wsdl 文件中的肥皂操作:
<soap:operation soapAction="sayHello"/>
OP 的代码返回:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://www.examples.com/wsdl/HelloService.wsdl">
<soap:Body>
<soap:Fault>
<soap:Code>
<soap:Value>SOAP-ENV:Server</soap:Value>
<soap:Subcode>
<soap:value>InternalServerError</soap:value>
</soap:Subcode>
</soap:Code>
<soap:Reason>
<soap:Text>TypeError: method is not a function</soap:Text>
</soap:Reason>
</soap:Fault>
</soap:Body>
</soap:Envelope>
wsdl 文件中存在错误。 肥皂标签未关闭。
<soap:address
location="http://localhost:8000/wsdl"/>
仅适用于新手,就像我对 Node.js
...将 JS 代码添加到index.js
并运行npm init -y
后跟 npm install soap -s
。
我还建议像这样为soap.listen
添加一个回调:
soap.listen(server, '/wsdl', helloService, xml, function() {
console.log('server initialized');
});
测试
如果你想测试(例如使用SoapUI),WSDL可以在 http://localhost:8000/wsdl?wsdl
)请求
(由 SoapUI 生成)
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:examples:helloservice">
<soapenv:Header/>
<soapenv:Body>
<urn:sayHello soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<firstName xsi:type="xsd:string">Betlista</firstName>
</urn:sayHello>
</soapenv:Body>
</soapenv:Envelope>
响应
如果实现更改为
sayHello: function(args) {
console.log("%j", args);
return {
greeting: "Hello " + args.firstName.$value + "!!!"
};
}
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="http://www.examples.com/wsdl/HelloService.wsdl">
<soap:Body>
<tns:sayHelloResponse>
<tns:greeting>Hello Betlista!!!</tns:greeting>
</tns:sayHelloResponse>
</soap:Body>
</soap:Envelope>
.log
$ node .
server initialized
args: {"attributes":{"soapenv:encodingStyle":"http://schemas.xmlsoap.org/soap/encoding/"},"firstName":{"attributes":{"xsi:type":"xsd:string"},"$value":"Betlista"}}
所以在args
我们收到了
{
"attributes": {
"soapenv:encodingStyle": "http://schemas.xmlsoap.org/soap/encoding/"
},
"firstName": {
"attributes": {
"xsi:type": "xsd:string"
},
"$value": "Betlista"
}
}