【问题标题】:soap client - server communication in Nodejs肥皂客户端 - Nodejs 中的服务器通信
【发布时间】:2023-03-20 19:47:01
【问题描述】:

我正在使用Strong-soap npm 包here 在Nodejs 中进行soap 客户端-服务器通信。我正在使用 AQL 查询处理 Arango 数据库。我正在尝试获取结果并将其显示在服务器端口中。

作为第一步,我创建了一个soap客户端文件和服务器文件,其中包含npm包中的示例。我不确定我在这里做错了什么?

User.js

// ##soap.listen
var another = require('./authen.js');
var soap = require('strong-soap').soap;
var http = require('http');

 var myService = {
      MyService: {
          MyPort: {
              check_username: function(args) {

          res.header("Access-Control-Allow-Origin", "*");
          res.header("Access-Control-Allow-Headers", "X-Requested-With");

    db.query(aqlQuery`
     LET startVertex = (FOR doc IN spec
     FILTER doc.serial_no == '"123456abcde"'
     LIMIT 2
     RETURN doc
    )[0]

   FOR v IN 1 ANY startVertex belongs_to
   RETURN v.ip`,
   {
    bindVar1: 'value',
    bindVar2: 'value',
  }
  )
    }
   }
  }
 };

  var xml = require('fs').readFileSync('myservice.wsdl', 'utf8'),
      server = http.createServer(function(request,response) {
          response.end("404: Not Found: " + request.url);
      });

  server.listen(8000);
  soap.listen(server, '/wsdl', myService, xml);

然后我创建了我的客户端文件以及我在下面共享的 wsdl 文件。一旦我运行了我的服务器和客户端文件。我收到一个功能无法识别的错误。我感觉我做错了什么。还是我做对了?

"use strict";

var soap = require('strong-soap').soap;

var url = 'http://192.00.00.000/test/myservice.wsdl';

var requestArgs = {
  symbol: 'IBM'
};

var options = {};

  soap.createClient(url, options, function(err, client) {

  var method = client['check_username'];

  method(requestArgs, function(err, result, envelope, soapHeader) {

    console.log('Response Envelope: \n' + envelope);
    console.log('Result: \n' + JSON.stringify(result));

});
})

错误:

  • var method = client['check_username'];
  • TypeError:无法读取未定义的属性“check_username”

【问题讨论】:

    标签: javascript node.js web-services soap wsdl


    【解决方案1】:

    我认为您在创建客户端对象时遇到了错误。我会说你应该检查soap.createClient调用中的err对象,例如

    var options = {};
    
    soap.createClient(url, options, function(err, client) {
    
        if (err) {
            console.error("An error has occurred creating SOAP client: " , err);  
        } else {
            // Log a description of the services the server offers.
            var description = client.describe();
            console.log("Client description:" , description);
            // Go on and call the method.
            var method = client['check_username'];
            method(requestArgs, function(err, result, envelope, soapHeader) {
                console.log('Response Envelope: \n' + envelope);
                console.log('Result: \n' + JSON.stringify(result));
            }
        }
    });
    

    记录 client.description 非常有用,您可以看到处理您的请求的服务器对象的结构。

    另外,客户端是否指向正确的 URL?你在服务器上监听 8000 端口,客户端指向这个吗?

    在服务器端,我会连接记录器,这可以告诉你发生了什么,例如你得到什么信封,GET 请求等。

    var soapServer = soap.listen(server, '/wsdl', myService, xml);
    
    soapServer.log = function(type, data) {
        // type is 'received' or 'replied'
        console.log('Type: ' + type + ' data: ' + data);
    };
    

    这是一个完整的例子:

    服务器

    var soap = require('strong-soap').soap;
    var http = require('http');
    
    var myService = {
        CheckUserName_Service: {
            CheckUserName_Port: {
                checkUserName: function(args, soapCallback) { 
                    console.log('checkUserName: Entering function..');
                    db.query(aqlQuery`
                    LET startVertex = (FOR doc IN spec
                    FILTER doc.serial_no == '"123456abcde"'
                    LIMIT 2
                    RETURN doc
                    )[0]
    
                    FOR v IN 1 ANY startVertex belongs_to
                    RETURN v.ip`,
                    {
                    bindVar1: 'value',
                    bindVar2: 'value',
                    }
                    ).then(function(response) {
                        console.log(`Retrieved documents.`, response._result);
                        soapCallback(JSON.stringify(response._result));
                    })
                    .catch(function(error) {
                        console.error('Error getting document', error);
                        soapCallback('Error getting document' + error.message);
                    });
                }
            }
        }   
    };
    
    
    var xml = require('fs').readFileSync('check_username.wsdl', 'utf8');
    
    var server = http.createServer(function(request,response) {
        response.end("404: Not Found: " + request.url);
    });
    
    var port = 8000;
    server.listen(port);
    
    var soapServer = soap.listen(server, '/test', myService, xml);
    soapServer.log = function(type, data) {
        console.log('Type: ' + type + ' data: ' + data);
    };
    
    console.log('SOAP service listening on port ' + port);
    

    客户

    "use strict";
    
    var soap = require('strong-soap').soap;
    var url = 'http://localhost:8000/test?wsdl';
    
    var options = { endpoint: 'http://localhost:8000/test'};
    var requestArgs = { userName: "TEST_USER" };
    soap.createClient(url, options, function(err, client) {
      if (err) {
          console.error("An error has occurred creating SOAP client: " , err);  
      } else {
          var description = client.describe();
          console.log("Client description:" , description);
          var method = client.checkUserName;
          method(requestArgs, function(err, result, envelope, soapHeader) {
            //response envelope
            console.log('Response Envelope: \n' + envelope);
            //'result' is the response body
            console.log('Result: \n' + JSON.stringify(result));
          });
      }
    });
    

    WSDL

    <definitions name = "CheckUserNameService"
       targetNamespace = "http://www.examples.com/wsdl/CheckUserNameService.wsdl"
       xmlns = "http://schemas.xmlsoap.org/wsdl/"
       xmlns:soap = "http://schemas.xmlsoap.org/wsdl/soap/"
       xmlns:tns = "http://www.examples.com/wsdl/CheckUserNameService.wsdl"
       xmlns:xsd = "http://www.w3.org/2001/XMLSchema">
    
       <message name = "CheckUserNameRequest">
          <part name = "userName" type = "xsd:string"/>
       </message>
       <message name = "CheckUserNameResponse">
          <part name = "status" type = "xsd:string"/>
       </message>
       <portType name = "CheckUserName_PortType">
          <operation name = "checkUserName">
             <input message = "tns:CheckUserNameRequest"/>
             <output message = "tns:CheckUserNameResponse"/>
          </operation>
       </portType>
    
       <binding name = "CheckUserName_Binding" type = "tns:CheckUserName_PortType">
          <soap:binding style = "rpc"
             transport = "http://schemas.xmlsoap.org/soap/http"/>
          <operation name = "checkUserName">
             <soap:operation soapAction = "checkUserName"/>
             <input>
                <soap:body encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" namespace = "urn:examples:CheckUserNameService" use = "encoded"/>
             </input>
             <output>
                <soap:body encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" namespace = "urn:examples:CheckUserNameService" use = "encoded"/>
             </output>
          </operation>
       </binding>
    
       <service name = "CheckUserName_Service">
          <documentation>WSDL File for CheckUserNameService</documentation>
          <port binding = "tns:CheckUserName_Binding" name = "CheckUserName_Port">
             <soap:address
                location = "http://www.examples.com/CheckUserName/" />
          </port>
       </service>
    </definitions>
    

    WSDL 应位于与服务器相同的目录中名为“check_username.wsdl”的文件中。

    【讨论】:

    • ..非常感谢您的详细回答和解释。 :)
    • 没问题,希望对你有帮助!!
    • 我刚刚实现了它..我有一个小问题..,在 WSDL 文件中,soap 位置地址应该不同吧??因为当我运行文件时,我在 localhost/test 的服务器中得到一个空屏幕
    • 如果您尝试 test?wsdl,您不会期望在 /test 看到任何内容,但您应该会看到 WSDL 文件。
    • 是的,我明白了。由于我是 wsdl 通信的新手。,我无法围绕工作的想法来思考......,就像它是如何真正工作的一样
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多