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

loopback-example-connector (REST)

Overview

This example demonstrates basic use of loopback-connector-rest.

The project has two servers: local-server and external-server. The “external” serverserves a simple REST API, while the local server fetches data usingthis REST API.

Running the app

  1. $ git clone https://github.com/strongloop-community/loopback-example-connector.git
  2. $ cd loopback-example-connector
  3. $ git checkout rest
  4. $ cd external-server
  5. $ npm install
  6. $ node .

In another shell:

  1. $ cd local-server
  2. $ npm install
  3. $ node .

You should see console messages on the local server. Verify the data has beenretrieved from the remote server by doing a GET request on Magazines in theexplorer or by running curl localhost:3000/api/Magazines.

FAQs

The following are common questions related to using the REST connector.

How do you perform a GET request to a remote server?

In this example, we have a REST API exposed in model-config.json.

To make a request to the remote server, declare new datasource that uses theREST connector in the local datasources.json.

A few things to note in this file is the connector property’s value is rest andthere is an operations property that takes an array of objects.

This object has two properties: template and function.

The template property contains method which is the HTTP method type toperform the request with and url which is the URL to the remote resource.

The function property is the name of the property you will use to trigger therequest. For example, we name our property find because we will trigger therequest using Magazine.find()….

The idea is to use find to make a GET request to a url we specify.

Tags: example_app