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

时间:2018-02-09 15:18:30

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

我使用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'

1 个答案:

答案 0 :(得分:1)

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

var soapServer = soap.listen(server, '/wsdl', myService, xml);

soapServer.log = function(type, data) {
    // type is 'received' or 'replied'
    console.log('Type: ' + type + ' data: ' + data);
};

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

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

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

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

<强>客户端

<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

{{1}}

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