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:
$ git clone https://github.com/strongloop-community/loopback-example-connector.git -b soap
$ cd loopback-example-connector
$ npm install
Run the example
- In the project root directory, run the app by entering this command:
node .
You’ll see this in the console:
Web server listening at: http://0.0.0.0:3000
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
andGET /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):
{
"GetAtomicNumberResult": "<NewDataSet>\r\n <Table>\r\n
<AtomicNumber>26</AtomicNumber>\r\n
<ElementName>Iron</ElementName>\r\n
<Symbol>Fe</Symbol>\r\n
<AtomicWeight>55.847</AtomicWeight>\r\n
<BoilingPoint>3300</BoilingPoint>\r\n
<IonisationPotential>7.9</IonisationPotential>\r\n
<EletroNegativity>1.6400000000000001</EletroNegativity>\r\n
<AtomicRadius>1.17</AtomicRadius>\r\n
<MeltingPoint>1808</MeltingPoint>\r\n
<Density>7874</Density>\r\n </Table>\r\n</NewDataSet>"
}
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:
$ 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:
$ 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?
Choose3.x (current)
What kind of application do you have in mind?
Chooseempty-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:
$ cd my-soap-demo
Use the data source generator to add a SOAP data source to your application. Enter this command:
$ 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 toSOAP 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:
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:
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.
{"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.0
Press Enter to install the connector from npm.The data source generator then creates an entry for the data source in theserver/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:
$ lb model
When prompted, respond as follows:
Enter the model name:
Enter “periodicTable”.Enter the model name: periodicTable
SelectsoapDS
that you previously created.Select model's base class
SelectModel
.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?
Selectserver
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 theserver
directory:periodicTable.json
andperiodicTable.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.
'use strict';
module.exports = function(Periodictable) {
// External PeriodTable WebService operation exposed as REST APIs through LoopBack
Periodictable.getAtomicnumber = function (elementName, cb) {
Periodictable.GetAtomicNumber({ElementName: elementName || 'Copper'}, function (err, response) {
var result = response;
cb(err, result);
});
};
// External PeriodTable WebService operation exposed as REST APIs through LoopBack
Periodictable.getAtomicweight = function(elementName, callback) {
Periodictable.GetAtomicWeight({ElementName: elementName || 'Copper'}, function (err, response) {
var result = response;
callback(err, result);
});
}
// Map to REST/HTTP
Periodictable.remoteMethod(
'getAtomicnumber', {
accepts: [
{arg: 'elementName', type: 'string', required: true,
http: {source: 'query'}}
],
returns: {arg: 'result', type: 'object', root: true},
http: {verb: 'get', path: '/GetAtomicNumber'}
}
);
Periodictable.remoteMethod(
'getAtomicweight', {
accepts: [
{arg: 'elementName', type: 'string', required: true,
http: {source: 'query'}}
],
returns: {arg: 'result', type: 'object', root: true},
http: {verb: 'get', path: '/GetAtomicWeight'}
}
);
};
Try it out!
Now, run your app:
node .
Follow the same steps as before and your app should behave the same as the one in this repository.
Tags: example_app