Page Contents

Add a Service


LB4 has the package @loopback/proxy-server that contains the artifacts needed to implement the link between the methods described in the .json file and the Node.js methods. All we need to do is to write the service provider that will serve as the glue to make this implementation real.

Installing the proxy-server

Make sure you are inside the soap-calculator directory and run the following command:

npm install @loopback/service-proxy -—save

Writing a service provider

Use the lb4 service command and the following inputs to create a calculator service:

lb4 service
? Please select the datasource CalculatorDatasource
? Service name: Calculator
   create src/services/calculator.service.ts
   update src/services/index.ts

Service Calculator was created in src/services/


import {getService} from '@loopback/service-proxy';
import {inject, Provider} from '@loopback/core';
import {CalculatorDataSource} from '../datasources';

export interface CalculatorService {
  // this is where you define the Node.js methods that will be
  // mapped to the SOAP operations as stated in the datasource
  // json file.

export class CalculatorServiceProvider implements Provider<CalculatorService> {
    // calculator must match the name property in the datasource json file
    protected dataSource: CalculatorDataSource = new CalculatorDataSource(),
  ) {}

  value(): Promise<CalculatorService> {
    return getService(this.dataSource);

Adding our interfaces

When we reviewed the remote SOAP web service, we found that there were four different results for the four operations and each of these operations were expecting the same pair of arguments intA and intB. Now, it is time to define this scenario using interfaces as follows:

export interface MultiplyResult {
  result: {
    MultiplyResult: number;
export interface AddResult {
  result: {
    AddResult: number;
export interface SubtractResult {
  result: {
    SubtractResult: number;
export interface DivideResult {
  result: {
    DivideResult: number;
export interface CalculatorParameters {
  intA: number;
  intB: number;

One important interface we need to add now is the one that describes the four Node.js methods that will be mapped to the SOAP operations. At this point we have just mentioned them in the .json data source file, so let’s add them now as follows:

export interface CalculatorService {
  Multiply(args: CalculatorParameters): Promise<MultiplyResult>;
  Add(args: CalculatorParameters): Promise<AddResult>;
  Divide(args: CalculatorParameters): Promise<DivideResult>;
  Subtract(args: CalculatorParameters): Promise<SubtractResult>;

Previous step: Add a Datasource

Next step: Add a Controller