使用Node.js实现SOAP协议的加法运算WebService

使用Node.js实现SOAP协议的加法运算WebService

下面是一个完整的示例,展示如何使用Node.js创建一个基于SOAP协议的WebService,实现加法运算功能。

1. 安装必要的依赖

首先,创建项目并安装所需包:

mkdir soap-addition-service
cd soap-addition-service
npm init -y
npm install express soap body-parser --save

2. 创建服务实现

创建 server.js 文件:

const express = require('express');
const bodyParser = require('body-parser');
const soap = require('soap');

const app = express();

// 中间件
app.use(bodyParser.raw({type: function(){return true;}, limit: '5mb'}));

// WSDL定义
const service = {
    CalculatorService: {
        CalculatorPort: {
            // 加法运算方法
            Add: function(args) {
                const n1 = parseFloat(args.a);
                const n2 = parseFloat(args.b);
                const result = n1 + n2;
                
                return {
                    result: result
                };
            }
        }
    }
};

// WSDL XML
const xml = require('fs').readFileSync('./calculator.wsdl', 'utf8');

// 启动服务
const port = 8000;
app.listen(port, function() {
    console.log('SOAP服务已启动,监听端口: ' + port);
    
    // 创建SOAP服务
    soap.listen(app, '/calculator', service, xml, function() {
        console.log('SOAP端点已准备好');
    });
});

3. 创建WSDL文件

创建 calculator.wsdl 文件:

<?xml version="1.0" encoding="UTF-8"?>
<definitions name="CalculatorService"
    targetNamespace="urn:CalculatorService"
    xmlns="http://schemas.xmlsoap.org/wsdl/"
    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:tns="urn:CalculatorService"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsd1="urn:CalculatorService">
    
    <types>
        <xsd:schema targetNamespace="urn:CalculatorService">
            <xsd:element name="AddRequest">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="a" type="xsd:float"/>
                        <xsd:element name="b" type="xsd:float"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="AddResponse">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="result" type="xsd:float"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:schema>
    </types>
    
    <message name="AddInput">
        <part name="parameters" element="tns:AddRequest"/>
    </message>
    <message name="AddOutput">
        <part name="parameters" element="tns:AddResponse"/>
    </message>
    
    <portType name="CalculatorPort">
        <operation name="Add">
            <input message="tns:AddInput"/>
            <output message="tns:AddOutput"/>
        </operation>
    </portType>
    
    <binding name="CalculatorBinding" type="tns:CalculatorPort">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="Add">
            <soap:operation soapAction="urn:CalculatorService#Add"/>
            <input>
                <soap:body use="literal"/>
            </input>
            <output>
                <soap:body use="literal"/>
            </output>
        </operation>
    </binding>
    
    <service name="CalculatorService">
        <port name="CalculatorPort" binding="tns:CalculatorBinding">
            <soap:address location="http://localhost:8000/calculator"/>
        </port>
    </service>
</definitions>

4. 创建客户端测试脚本

创建 client.js 文件用于测试:

const soap = require('soap');
const url = 'http://localhost:8000/calculator?wsdl';

const args = {a: 5, b: 3};

soap.createClient(url, function(err, client) {
    if (err) {
        console.error(err);
        return;
    }
    
    client.Add(args, function(err, result) {
        if (err) {
            console.error(err);
            return;
        }
        console.log('计算结果:', result.result);
    });
});

5. 运行和测试

  1. 启动服务:
node server.js
  1. 在另一个终端运行客户端测试:
node client.js

你应该会看到输出:计算结果: 8

6. 使用SOAP UI测试

你也可以使用SOAP UI等工具测试这个服务:

  1. 导入WSDL: http://localhost:8000/calculator?wsdl
  2. 创建请求:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:CalculatorService">
   <soapenv:Header/>
   <soapenv:Body>
      <urn:AddRequest>
         <a>10</a>
         <b>20</b>
      </urn:AddRequest>
   </soapenv:Body>
</soapenv:Envelope>
  1. 你应该会收到响应:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <ns1:AddResponse xmlns:ns1="urn:CalculatorService">
         <result>30</result>
      </ns1:AddResponse>
   </soap:Body>
</soap:Envelope>

总结

这个示例展示了如何使用Node.js创建一个简单的SOAP WebService,实现了加法运算功能。主要步骤包括:

  1. 定义WSDL文件描述服务接口
  2. 使用soap库创建SOAP服务
  3. 实现服务方法逻辑
  4. 创建客户端进行测试

你可以根据需要扩展这个服务,添加更多运算方法或更复杂的业务逻辑。

posted @ 2025-04-17 10:30  许仙儿  阅读(59)  评论(1)    收藏  举报