使用节点js的肥皂服务器

时间:2018-04-12 17:07:07

标签: node.js soap node-soap

我正在尝试使用节点js创建一个soap服务。似乎最常见的是使用此lib:https://www.npmjs.com/package/soap

他们有这个片段:

var myService = {
  MyService: {
      MyPort: {
          MyFunction: function(args) {
              return {
                  name: args.name
              };
          },

          // This is how to define an asynchronous function.
          MyAsyncFunction: function(args, callback) {
              // do some work
              callback({
                  name: args.name
              });
          },

          // This is how to receive incoming headers
          HeadersAwareFunction: function(args, cb, headers) {
              return {
                  name: headers.Token
              };
          },

          // You can also inspect the original `req`
          reallyDetailedFunction: function(args, cb, headers, req) {
              console.log('SOAP `reallyDetailedFunction` request from ' + req.connection.remoteAddress);
              return {
                  name: headers.Token
              };
          }
      }
  }
 };

  var xml = require('fs').readFileSync('myservice.wsdl', 'utf8');

 //http server example
 var server = http.createServer(function(request,response) {
  response.end('404: Not Found: ' + request.url);
 });

 server.listen(8000);
 soap.listen(server, '/wsdl', myService, xml);

 //express server example
 var app = express();
 //body parser middleware are supported (optional)
 app.use(bodyParser.raw({type: function(){return true;}, limit: '5mb'}));
 app.listen(8001, function(){
     //Note: /wsdl route will be handled by soap module
    //and all other routes & middleware will continue to work
    soap.listen(app, '/wsdl', myService, xml);
});

我的问题是。我是否需要手动生成此文件:myservice.wsdl,然后将其与MyService结构链接?

由于

1 个答案:

答案 0 :(得分:4)

是的,您需要自己创建WSDL文件。我相信任何可用的npm SOAP模块都需要这个。有各种工具可以帮助您生成WSDL,尽管最简单的方法之一就是简单地从一个简单的WSDL文件开始。例如与上面代码中的MyFunction调用相对应的文件:

<definitions name = "MyService"
   targetNamespace = "http://www.examples.com/wsdl/MyService.wsdl"
   xmlns = "http://schemas.xmlsoap.org/wsdl/"
   xmlns:soap = "http://schemas.xmlsoap.org/wsdl/soap/"
   xmlns:tns = "http://www.examples.com/wsdl/MyService.wsdl"
   xmlns:xsd = "http://www.w3.org/2001/XMLSchema">

   <message name = "MyFunctionRequest">
      <part name = "testParam" type = "xsd:string"/>
   </message>
   <message name = "MyFunctionResponse">
      <part name = "status" type = "xsd:string"/>
   </message>
   <portType name = "MyPort">
      <operation name = "MyFunction">
         <input message = "tns:MyFunctionRequest"/>
         <output message = "tns:MyFunctionResponse"/>
      </operation>
   </portType>

   <binding name = "MyFunction_Binding" type = "tns:MyPort">
      <soap:binding style = "rpc"
         transport = "http://schemas.xmlsoap.org/soap/http"/>
      <operation name = "MyFunction">
         <soap:operation soapAction = "MyFunction"/>
         <input>
            <soap:body encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" namespace = "urn:examples:MyService" use = "encoded"/>
         </input>
         <output>
            <soap:body encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" namespace = "urn:examples:MyService" use = "encoded"/>
         </output>
      </operation>
   </binding>

   <service name = "MyService">
      <documentation>WSDL File for MyService</documentation>
      <port binding = "tns:MyFunction_Binding" name = "MyPort">
         <soap:address
            location = "http://www.examples.com/MyFunction/" />
      </port>
   </service>

</definitions>

您可以使用以下客户端代码进行呼叫:

var soap = require('soap');
var url = 'http://localhost/wsdl?wsdl';
var args = {name: 'value'};
soap.createClient(url, function(err, client) {
  client.MyFunction(args, function(err, result) {
      console.log(result);
  });
});

另请查看Client.describe(),这非常有用,并将返回一个显示服务器支持的所有方法的对象。