NODEJS - Publish and Monitor a SOAP service

Requirements

Setup project (from CLI)

macosx / unix
window
> mkdir soap-service
> cd soap-sevice
> npm init 
> md soap-service
> cd soap-sevice
> npm init 

Install dependencies

 
> npm install http-proxy --save
> npm install git://github.com/bsorrentino/node-soap-server#develop --save 

Sample

open index.js (default main module) and copy the code below

index.js
var http = require('http'),
    httpProxy = require('http-proxy'),
    soap = require('soap-server'),
    util = require('util');


var host = {
 address:'127.0.0.1',
 port:{
   proxy:1337,
   server:1338

 },
 serverUri:function() {
   return util.format('http://%s:%d', this.address, this.port.server);
 },
 proxyUri:function() {
   return util.format('http://%s:%d', this.address, this.port.proxy);
 }
};

//
// Create a proxy server with custom application logic
//
var proxy = httpProxy.createProxyServer({});

//
// Create your custom server and just call `proxy.web()` to proxy
// a web request to the target passed in the options
// also you can use `proxy.ws()` to proxy a websockets request
//
var server = http.createServer(function(req, res) {
  // You can define here your custom logic to handle the request
  // and then proxy the request.
  console.log('Request', JSON.stringify(req.headers, true, 2));

  req.on('data', function (data) {
      console.dir( data.toString() );
  });

  proxy.web(req, res, { target: host.proxyUri() } );
});

proxy.on('proxyRes', function (proxyRes, req, res) {
  proxyRes.on('data', function (data) {
      console.dir( data.toString() );
  });

  console.log('Response', JSON.stringify(proxyRes.headers, true, 2));
});

///////////// SOAP SERVICE /////////////////////////////////
function MyTestService(){
}
MyTestService.prototype.test1 = function(myArg1, myArg2){
    console.log( "test1 ", "" + myArg1, "" + myArg2);

    return myArg1 + myArg2;
};

var soapServer = new soap.SoapServer();
var soapService = soapServer.addService('testService', new MyTestService());




/////// PROXY START
console.log("Proxy listening on port: ", host.port.proxy);
server.listen(host.port.proxy);

////// SOAP START
soapServer.listen(host.port.server, host.address);

console.log('Server running at ', host.port.server);
console.log("wsdl: ",host.proxyUri(), '/testService?wsdl');

start
> node ./main.js

Proxy listening on port:  1337
Server running at  1338
wsdl:  http://127.0.0.1:1337 /testService?wsdl
 
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.