求推介成熟的Nodejs soap server 模块,用于webservice开发。

求推介成熟的Nodejs soap server 模块,用于webservice开发。

最近刚接触Node.js 公司有一个项目要用Node.js 来作为服务端,而且是要做成webservice,这对于新手难度就挺大的了,现在有这么几个问题想请教一下,希望有过相关经验的或者对这方面有涉猎的朋友帮帮忙。有没有比较稳定,比较成熟的第三方模块。可以在此基础上构建自己的webservice?。

10 回复

求推介成熟的Nodejs SOAP Server模块,用于WebService开发

最近刚接触Node.js,公司有一个项目需要使用Node.js来作为服务端,并且要实现一个WebService。这对新手来说难度较大。以下是我遇到的一些问题,希望能得到有相关经验的朋友的帮助。

1. 稳定的第三方模块推荐

在Node.js中,实现SOAP服务器的一个成熟且稳定的第三方模块是soap。这个模块提供了一套完整的解决方案来创建和处理SOAP Web服务。

2. 示例代码

以下是一个简单的示例,展示如何使用soap模块创建一个基本的SOAP服务。

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

// 定义一个简单的服务
const services = {
    HelloWorldService: {
        HelloWorldPort: {
            SayHello: function(args) {
                return {
                    Greeting: `Hello, ${args.name}`
                };
            }
        }
    }
};

// 定义WSDL文件(这里简化为字符串)
const wsdl = `
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:tns="http://tempuri.org/"
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    targetNamespace="http://tempuri.org/">
    <wsdl:types>
        <xsd:schema targetNamespace="http://tempuri.org/">
            <xsd:element name="SayHelloRequest">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="name" type="xsd:string"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="SayHelloResponse">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="Greeting" type="xsd:string"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:schema>
    </wsdl:types>
    <wsdl:message name="SayHelloSoapIn">
        <wsdl:part name="parameters" element="tns:SayHelloRequest"/>
    </wsdl:message>
    <wsdl:message name="SayHelloSoapOut">
        <wsdl:part name="parameters" element="tns:SayHelloResponse"/>
    </wsdl:message>
    <wsdl:portType name="HelloWorldPortType">
        <wsdl:operation name="SayHello">
            <wsdl:input message="tns:SayHelloSoapIn"/>
            <wsdl:output message="tns:SayHelloSoapOut"/>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="HelloWorldSoapBinding" type="tns:HelloWorldPortType">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="SayHello">
            <soap:operation soapAction="http://tempuri.org/SayHello" style="document"/>
            <wsdl:input>
                <soap:body use="literal"/>
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="HelloWorldService">
        <wsdl:port name="HelloWorldPort" binding="tns:HelloWorldSoapBinding">
            <soap:address location="http://localhost:8000/wsdl"/>
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>
`;

// 创建HTTP服务器
http.createServer(function (req, res) {
    // 解析请求并调用相应的服务方法
    soap.listen(req, res, services, wsdl);
}).listen(8000);

console.log('Server listening on port 8000');

3. 说明

  • soap模块:这是一个非常强大的模块,可以用来处理SOAP协议的请求和响应。
  • services对象:定义了服务的方法和逻辑。
  • wsdl字符串:定义了服务的WSDL描述,包括输入和输出消息、操作等。
  • http.createServer:创建了一个HTTP服务器,监听端口8000。
  • soap.listen:将SOAP服务绑定到HTTP服务器上。

通过上述代码,你可以创建一个简单的SOAP服务,处理基本的请求和响应。希望这能帮助你开始你的Node.js SOAP服务开发之旅!


没稳定的,你可以试着在npmjs.org 下一个下来,自己再测试,修改,完善。

restful 的接触过,soap好老啊。

谢谢各位,先去试试

npmjs.org https://www.npmjs.org/package/soap-server github: https://github.com/vpulim/node-soap 找到这两个,看了一下,有需要的也可以去看看。

node 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
              })
          }
      }
  }

} 这个对象里面嵌套那么多层,可以去掉几层吗?

soap性能太差,现在一般都用http+json,或者http+hessian

谢谢各位,我的webservice已经搞定了

推荐成熟的Node.js SOAP Server模块

在Node.js中,处理SOAP服务的一种成熟且常用的模块是soap。该模块功能强大、文档齐全,并且已经被广泛使用和验证。

示例代码

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

  1. 安装依赖

    首先,你需要安装soap模块。你可以通过npm来安装它:

    npm install soap
    
  2. 编写服务逻辑

    假设你要提供一个简单的数学服务,包含加法和减法操作:

    const soap = require('soap');
    const http = require('http');
    
    // 定义服务逻辑
    const service = {
      MathService: {
        MathPort: {
          Add(args) {
            return { Result: args.A + args.B };
          },
          Subtract(args) {
            return { Result: args.A - args.B };
          }
        }
      }
    };
    
    // 服务描述文件 (WSDL)
    const wsdl = `
    <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                     xmlns:tns="http://example.com/math"
                     xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                     targetNamespace="http://example.com/math">
      <wsdl:types>
        <xsd:schema targetNamespace="http://example.com/math">
          <xsd:element name="Add" type="tns:Add"/>
          <xsd:element name="AddResponse" type="tns:AddResponse"/>
          <xsd:complexType name="Add">
            <xsd:sequence>
              <xsd:element name="A" type="xsd:int"/>
              <xsd:element name="B" type="xsd:int"/>
            </xsd:sequence>
          </xsd:complexType>
          <xsd:complexType name="AddResponse">
            <xsd:sequence>
              <xsd:element name="Result" type="xsd:int"/>
            </xsd:sequence>
          </xsd:complexType>
          <!-- Subtract 类似 -->
        </xsd:schema>
      </wsdl:types>
    </wsdl:definitions>
    `;
    
    // 创建HTTP服务器并绑定服务
    const server = http.createServer((req, res) => {
      soap.listen(server, '/wsdl', service, wsdl);
    });
    
    // 启动服务器
    server.listen(8000, () => {
      console.log('Server listening on port 8000...');
    });
    
  3. 运行服务器

    运行上述脚本后,你的SOAP服务器将在本地的8000端口上运行,并可以通过http://localhost:8000/wsdl访问。

总结

soap模块是Node.js中一个非常强大的工具,可以用来快速搭建稳定的SOAP服务。上面的示例代码展示了如何定义服务逻辑、编写WSDL描述文件以及启动SOAP服务器。这个模块非常适合需要进行SOAP通信的应用场景。

如果你需要进一步定制或扩展功能,soap模块也提供了丰富的API和文档供参考。

回到顶部