如何开始使用node-soap

时间:2013-12-23 09:54:02

标签: node.js web-services soap wsdl soapserver

我尝试使用node-soap使用node.js制作soap-server。 我喜欢

<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());               ^ 错误:标签不匹配

如何修复它。

3 个答案:

答案 0 :(得分:2)

var helloService = {
  Hello_Service: {
    Hello_Port: {
      sayHello: function(args) {
        return {
          greeting: "Hello!!!"
        };
      }
    }
  }
}

在您的代码中修复此问题(您的代码中的名称方法和输出参数错误)。 sayHello这个soapAction在wsdl文件中。

答案 1 :(得分:1)

wsdl文件中存在错误。肥皂标签未关闭。

<soap:address
        location="http://localhost:8000/wsdl"/>

答案 2 :(得分:0)

对于新手来说,就像我在使用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),则可以在http://localhost:8000/wsdl?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>

日志

$ 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"
    }
}