【问题标题】:How to get started node-soap如何开始使用 node-soap
【发布时间】:2013-12-23 09:54:02
【问题描述】:

我尝试使用 node-soap 使用 node.js 制作肥皂服务器。 我喜欢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()); ^ 错误:标签不匹配

我该如何解决。

【问题讨论】:

  • 你是如何创建 wsdl 的。是你自己写的吗?你知道是否可以用 node 生成这个吗?
  • 我是从某个地方抄来的教程。

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


【解决方案1】:

除了vinayr's answer,把函数改成

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

在您的代码中修复此问题(代码中的方法名称和输出参数错误)。

sayHello是wsdl文件中的soapAction:

<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>

【讨论】:

    【解决方案2】:

    您的 wsdl 文件中有错误。肥皂标签未关闭。

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

    【讨论】:

      【解决方案3】:

      仅适用于新手,就像我使用 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>
      

      日志

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-20
        • 1970-01-01
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多