node.js的soap 服务端

本文介绍如何利用Node.js的soap库创建一个SOAP服务端。通过定义WSDL和实现服务端逻辑,演示了如何监听HTTP服务器并响应SOAP请求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

const soap = require("soap");

const http = require("http");

const webservice = {};

      webservice.wsdl = `

      <?xml version="1.0"?>

      <wsdl:definitions name="StockQuote" targetNamespace="http://example.com/stockquote.wsdl" xmlns:tns="http://example.com/stockquote.wsdl" xmlns:xsd1="http://example.com/stockquote.xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">

          <wsdl:types>

            <xsd:schema targetNamespace="http://example.com/stockquote.xsd" xmlns:xsd="http://www.w3.org/2000/10/XMLSchema">

                <xsd:element name="WriteObjectRequest">

                    <xsd:complexType>

                        <xsd:all>

                            <xsd:element name="xtlb" type="string"/>

                            <xsd:element name="jkid" type="string"/>

                            <xsd:element name="xmlDoc" type="string"/>

                            <xsd:element name="yhdh" type="string"/>

                            <xsd:element name="mm" type="string"/>

                        </xsd:all>

                    </xsd:complexType>

                </xsd:element>

                <xsd:element name="WriteObjectResponse">

                    <xsd:complexType>

                        <xsd:all>

                            <xsd:element name="WriteObjectOutResult" type="string"/>

                        </xsd:all>

                    </xsd:complexType>

                </xsd:element>

                <xsd:element name="TradePriceSubmit">

                    <xsd:complexType>

                        <xsd:all>

                            <xsd:element name="xtlb" type="string"/>

                            <xsd:element name="jkid" type="string"/>

                            <xsd:element name="xmlDoc" type="string"/>

                            <xsd:element name="yhdh" type="string"/>

                            <xsd:element name="mm" type="string"/>

                            <xsd:element name="WriteObjectOutResult" type="string"/>

                        </xsd:all>

                    </xsd:complexType>

                </xsd:element>

                <xsd:element name="valid" type="boolean"/>

            </xsd:schema>

          </wsdl:types>

          <wsdl:message name="WriteObjectInput">

              <wsdl:part name="body" element="xsd1:WriteObjectRequest"/>

          </wsdl:message>

          <wsdl:message name="WriteObjectOutput">

              <wsdl:part name="body" element="xsd1:WriteObjectResponse"/>

          </wsdl:message>

          <wsdl:portType name="StockQuotePortType">

              <wsdl:operation name="WriteObject">

                <wsdl:input message="tns:WriteObjectInput"/>

                <wsdl:output message="tns:WriteObjectOutput"/>

              </wsdl:operation>

          </wsdl:portType>

          <wsdl:binding name="StockQuoteSoapBinding" type="tns:StockQuotePortType">

              <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

              <wsdl:operation name="WriteObject">

                <soap:operation soapAction="http://example.com/WriteObject"/>

                <wsdl:input>

                    <soap:body use="literal"/>

                </wsdl:input>

                <wsdl:output>

                    <soap:body use="literal"/>

                </wsdl:output>

              </wsdl:operation>

          </wsdl:binding>

          <wsdl:service name="StockQuoteService">

              <wsdl:port name="StockQuotePort" binding="tns:StockQuoteSoapBinding">

                <soap:address location="http://localhost:8001/HBService"/>

              </wsdl:port>

          </wsdl:service>

      </wsdl:definitions>`;

      webservice.server = null;

      webservice.service = {

        StockQuoteService: {

          StockQuotePort: {

            WriteObject: async (args, cb, soapHeader) => {

              const myServer = new MyServer(ctx);

              let res = await myServer.receiveData(args);

              return { WriteObjectOutResult: res };

            }

          }

        }

      };

      webservice.server = http.createServer(function (request, response) {

        response.end("404: Not Found: " + request.url);

      });

      webservice.server.listen(8001, null, null, function () {

        webservice.soapServer = soap.listen(webservice.server, "/HBService", webservice.service, webservice.wsdl);

        webservice.baseUrl = "http://" + webservice.server.address().address + ":" + webservice.server.address().port;

        if (webservice.server.address().address === "0.0.0.0" || webservice.server.address().address === "::") {

          webservice.baseUrl = "http://127.0.0.1:" + webservice.server.address().port;

        }

      });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值