I know what you’re thinking. “Are you serious? Why? Node and SOAP are completely different things. Are you high?” But in working with large companies that use older web technologies, I could not avoid having to interact with SOAP*. (Don’t even get me started on why I dislike languages like Java, C++, or .NET)
No I am not high and yes I know that Node and SOAP work in very different ways. Node is async and SOAP is synchronous. Node is lightweight, and SOAP is as heavy as Ser Gregor Clegane.
I had the (unfortunate or fortune, depending what kind of dev you ask) of having to architect a way for Node to make a SOAP call. Using node-soap and Jowanza Joseph’s blog post, I was able to initiate a response.
I assume if you’re reading this, you’re familiar with require statements in node, but if not, make sure to require(‘node-soap’) in your files.
Working in lightweight tech has shown me how web services were done before my time. Enter the wsdl. It’s heavy. It’s cumbersome. It’s verbose. But its an important piece to the SOAP puzzle. If you open the wsdl, you’ll notice its an XML-defined schema.
For the purposes of this blog post, I’ll use an open wsdl available on the interwebz, the Global Weather WSDL.
Node-soap requires that a soap client be created that will allow our request to be exposed to the methods defined in the wsdl. soapCreateClient takes 3 arguments.
My example looks like this:
Calling the SOAP methods
The next step is to have our client call the methods defined in the wsdl. It should follow this protocol:
method is the name of the method you are trying to call that is found in the wsdl. For my example, I want to get city weather by ZIP code. My example looks like this:
And like a good Node developer, I add error handling.
To wrap up, I leave some truth from Jowanza Joseph:
“I won’t pretend like it’s _easy _to deal with XML/ SOAP based APIs in Node.js, I wrote this post to illustrate that it’s doable. There are other folks dealing with the same issues and its not likely to go away anytime soon. The more examples we post online the more we can help each other and make the web a better place.”
Stay strong my JS heads!