代码之家  ›  专栏  ›  技术社区  ›  cantona_7

Nodejs中的soap客户机-服务器通信

  •  0
  • cantona_7  · 技术社区  · 6 年前

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

    作为第一步,我创建了一个soap客户机文件和服务器文件,其中包含npm包中提供的示例。我不知道我做错了什么?

    使用者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=客户端['check\u username'];
    • TypeError:无法读取未定义的属性“check\u username”
    1 回复  |  直到 6 年前
        1
  •  2
  •   Terry Lennox    6 年前

    我认为您在创建客户端对象时出错。我建议您检查soap中的err对象。createClient调用,例如。

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

    记录客户端。说明非常有用,您可以看到处理请求的服务器对象的结构。

    此外,客户端是否指向正确的URL?您正在服务器上的端口8000上侦听,客户端是否指向此端口?

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

    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\u username”的文件中。与服务器位于同一目录中。