cantona_7
cantona_7

Reputation: 1177

soap client - server communication in Nodejs

I am using Strong- soap npm package here to make soap client-server communication in Nodejs. I am dealing with Arango database using AQL queries. I ma trying to get the result and display it in the server port.

As a first step, I have created a soap client file and server file with examples available in npm package. I am not sure what am I doing wrong here ?

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

Then I have created my client side file along with the wsdl file which I have shared below. Once I run both my server and client file. I am getting a error that doesn't recogonize by function. I getting a feeling that I am doing somethin wrong.Or am I doing it in a right way ?

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

});
})

Error:

Upvotes: 0

Views: 8102

Answers (1)

Terry Lennox
Terry Lennox

Reputation: 30675

I think you're getting an error creating your client object. I'd say you should check the err object in the soap.createClient call, e.g.

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

Logging the client.description is very useful, you can see the structure of the server object handling your requests.

Also, is the client pointing at the correct URL? You're listening on port 8000 on the server, is the client pointed to this?

On the server side, I'd hook up the logger, this can tell you what's going on, e.g. what envelopes you're getting, GET requests etc.

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

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

Here's a complete example:

Server

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

Client

"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>

The WSDL should be in a file called 'check_username.wsdl' in the same directory as the server.

Upvotes: 2

Related Questions