想了解下有人用过Nodejs去写SOAP的服务器端吗?

想了解下有人用过Nodejs去写SOAP的服务器端吗?

想了解下有人用过nodejs去写SOAP的服务器端吗? node-soap 的现有库支持怎么样?测试啦下 ndoe-soap必须要进行WSDL书写比较麻烦? 有么有类似ASIX2 的库支持!

4 回复

当然可以。首先,我们需要理解SOAP(简单对象访问协议)是一种基于XML的协议,用于在网络上交换结构化数据。而Node.js是一种运行在服务端的JavaScript平台,它可以帮助我们处理网络请求。为了使用Node.js来创建SOAP服务器,我们可以使用node-soap库。

使用 node-soap 创建 SOAP 服务器

node-soap 是一个流行的库,用于在Node.js中处理SOAP请求。下面是一个简单的例子,展示如何使用node-soap创建一个基本的SOAP服务器:

  1. 安装 node-soap

    首先,你需要安装node-soap。你可以通过npm(Node包管理器)来安装它:

    npm install node-soap
    
  2. 创建 WSDL 文件

    为了让SOAP服务正常工作,你需要提供一个WSDL文件,它描述了你的服务接口。这里是一个非常基础的例子:

    <?xml version="1.0"?>
    <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                     xmlns:tns="http://example.com/soapserver"
                     xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                     targetNamespace="http://example.com/soapserver">
      <wsdl:types>
        <xsd:schema targetNamespace="http://example.com/soapserver">
          <xsd:element name="addRequest" type="xsd:int"/>
          <xsd:element name="addResponse" type="xsd:int"/>
        </xsd:schema>
      </wsdl:types>
      <wsdl:message name="addRequestMessage">
        <wsdl:part name="parameters" element="tns:addRequest"/>
      </wsdl:message>
      <wsdl:message name="addResponseMessage">
        <wsdl:part name="parameters" element="tns:addResponse"/>
      </wsdl:message>
      <wsdl:portType name="AddPortType">
        <wsdl:operation name="add">
          <wsdl:input message="tns:addRequestMessage"/>
          <wsdl:output message="tns:addResponseMessage"/>
        </wsdl:operation>
      </wsdl:portType>
      <wsdl:binding name="AddBinding" type="tns:AddPortType">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="add">
          <soap:operation soapAction="urn:add"/>
          <wsdl:input>
            <soap:body use="literal" namespace="http://example.com/soapserver"/>
          </wsdl:input>
          <wsdl:output>
            <soap:body use="literal" namespace="http://example.com/soapserver"/>
          </wsdl:output>
        </wsdl:operation>
      </wsdl:binding>
      <wsdl:service name="AddService">
        <wsdl:port name="AddPort" binding="tns:AddBinding">
          <soap:address location="http://localhost:8000/soapserver"/>
        </wsdl:port>
      </wsdl:service>
    </wsdl:definitions>
    
  3. 编写 SOAP 服务器代码

    现在,我们可以使用上面的WSDL定义来创建一个简单的SOAP服务器:

    const soap = require('soap');
    const http = require('http');
    
    // 定义服务逻辑
    const service = {
      AddService: {
        AddPort: {
          add(args) {
            return { addResponse: args.addRequest + 1 };
          }
        }
      }
    };
    
    // 创建HTTP服务器
    const server = http.createServer((req, res) => {
      res.end('404: Not Found: ' + req.url);
    });
    
    server.listen(8000, () => {
      console.log('Server listening on port 8000');
    });
    
    // 将服务与WSDL绑定到HTTP服务器上
    soap.listen(server, '/soapserver', service, './path/to/your.wsdl', (err) => {
      if (err) throw err;
      console.log('Server started successfully');
    });
    

这个例子展示了如何使用node-soap创建一个简单的SOAP服务器,该服务器可以响应add操作。你可以根据需要扩展这个例子,添加更多的操作和服务。虽然手动编写WSDL可能会有些繁琐,但这是确保SOAP服务正确配置的关键步骤。


node-soap你使用的是哪个版本,0.3版本的需要本地编译环境,当然编译好的就可以一直使用了。0.5的版本去掉了带有贬义依赖的包,使得使用更加的灵活方便。但是0.5的包依然存在很多的问题,至于常规的使用已经是差不多了。 我们自己项目中使用的就是node-soap,并且封装在了meteor.js中,一部分bug自行修改了,有的bug提交上去了。相对node-soap来说现在改善的很不错了,应该没有什么太大的问题。 另外我对WebServices不是很了解,使用的协议也不是很多,满足项目要求的基本协议都是可用的,以上。

Node.js 社区中确实有一些库可以用来实现 SOAP 服务端,node-soap 是最常用的之一。不过,它确实需要编写 WSDL(Web Services Description Language)文件来描述你的服务接口。虽然这可能显得有些繁琐,但这是为了确保客户端能够正确地与你的服务进行交互。

如果你觉得手动编写 WSDL 文件太麻烦,可以考虑使用一些工具来自动生成 WSDL 文件,或者寻找现成的解决方案。对于更高级的需求,你也可以考虑扩展 node-soap 或者探索其他库。

以下是一个简单的示例,展示如何使用 node-soap 创建一个基本的 SOAP 服务器:

示例代码

  1. 安装依赖
npm install soap
  1. 创建服务

假设我们有一个简单的数学服务,包含加法和减法两个操作:

const soap = require('soap');
const http = require('http');

// 定义我们的服务逻辑
const mathService = {
  MathService: {
    MathPort: {
      Add(args) {
        return { Result: args.A + args.B };
      },
      Subtract(args) {
        return { Result: args.A - args.B };
      }
    }
  }
};

// 创建一个简单的 HTTP 服务器
const server = http.createServer((request, response) => {
  response.end('404: Not Found: ' + request.url);
});

server.listen(8000, () => {
  // 将服务暴露给 SOAP 客户端
  const xml = require('fs').readFileSync('mathService.wsdl', 'utf8');
  soap.listen(server, '/wsdl', mathService, xml, () => {
    console.log('WSDL service is running at http://localhost:8000/wsdl');
  });
});
  1. WSDL 文件

这里是一个简单的 WSDL 文件 mathService.wsdl

<definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
             xmlns:tns="http://example.com/math"
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
             targetNamespace="http://example.com/math">
  <types>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example.com/math">
      <xs:element name="AddRequest">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="A" type="xs:int"/>
            <xs:element name="B" type="xs:int"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="AddResponse">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="Result" type="xs:int"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <!-- Subtract 类似 -->
    </xs:schema>
  </types>
  <message name="AddRequestMessage">
    <part name="parameters" element="tns:AddRequest"/>
  </message>
  <message name="AddResponseMessage">
    <part name="parameters" element="tns:AddResponse"/>
  </message>
  <portType name="MathPortType">
    <operation name="Add">
      <input message="tns:AddRequestMessage"/>
      <output message="tns:AddResponseMessage"/>
    </operation>
    <!-- Subtract 类似 -->
  </portType>
  <binding name="MathBinding" type="tns:MathPortType">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="Add">
      <soap:operation soapAction="http://example.com/math/Add"/>
      <input>
        <soap:body use="literal"/>
      </input>
      <output>
        <soap:body use="literal"/>
      </output>
    </operation>
    <!-- Subtract 类似 -->
  </binding>
  <service name="MathService">
    <port name="MathPort" binding="tns:MathBinding">
      <soap:address location="http://localhost:8000/wsdl"/>
    </port>
  </service>
</definitions>

通过以上步骤,你可以设置一个基本的 SOAP 服务。如果需要更复杂的功能,可以继续扩展和优化代码。

回到顶部