Example of using the LoopBack SOAP connector.Note: This page was generated from the loopback-example-connector/README.md.

See also:

loopback-example-connector (SOAP)

The SOAP connector enables LoopBack applications to interact withSOAP-based web services described usingWSDL. This example app illustrates calling a periodic table SOAP web service from a LoopBack app,where you define a remote method to call each SOAP web service operation.This is a simple example of a LoopBack app “proxying” or intermediating a web service.

For more information, see theSOAP connector documentation.

Installation

Clone this repo and install dependencies from npm:

  1. $ git clone https://github.com/strongloop-community/loopback-example-connector.git -b soap
  2. $ cd loopback-example-connector
  3. $ npm install

Run the example

  • In the project root directory, run the app by entering this command:
  1. node .

You’ll see this in the console:

  1. Web server listening at: http://0.0.0.0:3000
  2. Browse your REST API at http://0.0.0.0:3000/explorer
  • Open API Explorer in your web browser.
  • Under the periodicTable model, you’ll see two endpoints: GET /periodicTables/GetAtomicNumber and GET /periodicTables/GetAtomicWeight.
  • Click on GetAtomicNumber and in the elementName parameter field, enter the name of an element, for example, “Gold” or “Oxygen”. In the Response Body field, you’ll see an XML response from the web service that looks something like this (NOTE: line feeds have been added in the example below for readability, but in practice the response will be on a single line):
  1. {
  2. "GetAtomicNumberResult": "<NewDataSet>\r\n <Table>\r\n
  3. <AtomicNumber>26</AtomicNumber>\r\n
  4. <ElementName>Iron</ElementName>\r\n
  5. <Symbol>Fe</Symbol>\r\n
  6. <AtomicWeight>55.847</AtomicWeight>\r\n
  7. <BoilingPoint>3300</BoilingPoint>\r\n
  8. <IonisationPotential>7.9</IonisationPotential>\r\n
  9. <EletroNegativity>1.6400000000000001</EletroNegativity>\r\n
  10. <AtomicRadius>1.17</AtomicRadius>\r\n
  11. <MeltingPoint>1808</MeltingPoint>\r\n
  12. <Density>7874</Density>\r\n </Table>\r\n</NewDataSet>"
  13. }

Additional example code

The app includes two other files periodictable-ws.js and stock-ws.js that illustrate other ways of programmatically calling a SOAP web service. NOTE: the stock quote web service may not be consistently available.

Recreate the app

Prerequisites: If you haven’t already done so, follow the Installation instructions for the LoopBack CLI. In a nutshell:

  1. $ npm install -g loopback-cli

To create this app yourself, follow the steps in this section:

Scaffold the app

Scaffold a new application. Enter this command:

  1. $ lb app

When prompted, respond as follows:

  • What's the name of your application?Give the app any name you wish, for example “my-soap-demo”.The tool will create a directory with that name (my-soap-demo).
  • Enter name of the directory to contain the project:Press Enter to accept the default (directory has the same name as the app).
  • Which version of LoopBack would you like to use?Choose 3.x (current)
  • What kind of application do you have in mind?Choose empty-server (An empty LoopBack API, without any configured models or datasources) The tool will then scaffold the app and install all the dependencies from npm.

Create a SOAP data source

Go into the app root directory:

  1. $ cd my-soap-demo

Use the data source generator to add a SOAP data source to your application. Enter this command:

  1. $ lb datasource

When prompted, respond as follows:

  • Enter the datasource name:Enter “soapDS”.
  • Select the connector for soapDS:Use your arrow key to scroll down to SOAP webservices (supported by StrongLoop) and press Enter.
  • URL to the SOAP web service endpoint:Copy and paste the URL of the periodic table web service:
  1. http://www.webservicex.net/periodictable.asmx
  • HTTP URL or local file system path to the WSDL file:Copy and paste this URL of the periodic table web service WSDL:
  1. http://www.webservicex.net/periodictable.asmx?WSDL
  • Expose operations as REST APIs: (Y/n)Press Enter to accept the default (yes).
  • Maps WSDL binding operations to Node.js methods:Copy and paste the stringified JSON below. The JSON defines the service, port, and operation for each operation that will be called in the SOAP service.
  1. {"getAtomicWeight":{"service":"periodictable","port":"periodictableSoap","operation":"GetAtomicWeight"},"getAtomicNumber":{"service":"periodictable","port":"periodictableSoap","operation":"GetAtomicNumber"}}

NOTE: The JSON you enter must not have any line endings, that is, it must be on a single line.

  • Install loopback-connector-soap@^3.0Press Enter to install the connector from npm.The data source generator then creates an entry for the data source in the server/datasources.json file and installs all the necessary dependencies.

Create a periodicTable model

Use the model generator to add a model to represent the periodic Table web service. Enter this command:

  1. $ lb model

When prompted, respond as follows:

  • Enter the model name: Enter “periodicTable”.
  • Enter the model name: periodicTable Select soapDS that you previously created.
  • Select model's base class Select Model.
  • Expose periodicTable via the REST API? (Y/n) Press Enter to accept the default (yes).
  • Custom plural form (used to build REST URL): Press Enter for no custom plural.
  • Common model or server only? Select server because this will be a server-only model.
  • Property name: Press Enter when first prompted, because this model will not have any properties.The tool will create two files in the server directory: periodicTable.json and periodicTable.js.

Add remote methods

Edit server/models/periodic-table.js and add the code shown below to the stubbed-out function.

This code defines two functions, Periodictable.getAtomicnumber and Periodictable.getAtomicweight and adds them as remote methods to the Periodictable model, as described in Remote methods.

  1. 'use strict';
  2. module.exports = function(Periodictable) {
  3. // External PeriodTable WebService operation exposed as REST APIs through LoopBack
  4. Periodictable.getAtomicnumber = function (elementName, cb) {
  5. Periodictable.GetAtomicNumber({ElementName: elementName || 'Copper'}, function (err, response) {
  6. var result = response;
  7. cb(err, result);
  8. });
  9. };
  10. // External PeriodTable WebService operation exposed as REST APIs through LoopBack
  11. Periodictable.getAtomicweight = function(elementName, callback) {
  12. Periodictable.GetAtomicWeight({ElementName: elementName || 'Copper'}, function (err, response) {
  13. var result = response;
  14. callback(err, result);
  15. });
  16. }
  17. // Map to REST/HTTP
  18. Periodictable.remoteMethod(
  19. 'getAtomicnumber', {
  20. accepts: [
  21. {arg: 'elementName', type: 'string', required: true,
  22. http: {source: 'query'}}
  23. ],
  24. returns: {arg: 'result', type: 'object', root: true},
  25. http: {verb: 'get', path: '/GetAtomicNumber'}
  26. }
  27. );
  28. Periodictable.remoteMethod(
  29. 'getAtomicweight', {
  30. accepts: [
  31. {arg: 'elementName', type: 'string', required: true,
  32. http: {source: 'query'}}
  33. ],
  34. returns: {arg: 'result', type: 'object', root: true},
  35. http: {verb: 'get', path: '/GetAtomicWeight'}
  36. }
  37. );
  38. };

Try it out!

Now, run your app:

  1. node .

Follow the same steps as before and your app should behave the same as the one in this repository.

Tags: example_app