node.js - 使用 Node js 的 Soap 服务器

标签 node.js soap node-soap

我正在尝试使用 Node js 创建一个 soap 服务。看起来最常见的做法是使用这个库: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 链接?

谢谢

最佳答案

是的,您需要自己创建 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(),这非常有用,会返回一个显示服务器支持的所有方法的对象。

关于node.js - 使用 Node js 的 Soap 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49802327/

相关文章:

javascript - Node.js Streams - 及时安排事件而不产生背压

node.js - 如何在NextJS Vercel项目中进行301重定向?

web-services - Delphi SOAP 客户端不能保持超过 2 个并发请求。如何增加?

node.js - 如何在 node-soap 中使用派生类型

node.js - 如何使用 node-soap 库将文件附件添加到 soap 请求?

node.js - 使用 express/connect 和 session 存储时如何找到 session ID?

javascript - 如何在 NodeJS 中添加对 Web 服务的引用

javascript - 如何使用 Node-ews 集成 Exchange 2010 版本的投票选项?

java - 从 wsdl 创建模拟 soap web 服务 - Java

javascript - 在 Node.js 中处理来自 SOAP Web 服务调用的字符串响应的最佳方法