Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

loopback-connector-remote

kraman98.4kMIT3.4.1

Remote REST API connector for Loopback

web, restful, rest, StrongLoop

readme

loopback-connector-remote

The remote connector enables you to use a LoopBack application as a data source via REST. You can use the remote connector with a LoopBack application, a Node application, or a browser-based application that uses LoopBack in the client. The connector uses Strong Remoting.

In general, using the remote connector is more convenient than calling into REST API, and enables you to switch the transport later if you need to.

Use loopback-connector-remote:

  • Version 3.x with LoopBack v3 and later.
  • Prior versions with LoopBack v2.

Installation

In your application root directory, enter:

$ npm install loopback-connector-remote --save

This will install the module and add it as a dependency to the application's package.json file.

Creating a remote data source

Create a new remote data source with the datasource generator:

$ lb datasource

When prompted:

  • For connector, scroll down and select other.
  • For connector name without the loopback-connector- prefix, enter remote.

This creates an entry in datasources.json; Then you need to edit this to add the data source properties, for example:

{% include code-caption.html content="/server/datasources.json" %}

...
 "myRemoteDataSource": {
    "name": "myRemoteDataSource",
    "connector": "remote",
    "url": "http://localhost:3000/api"
  }
 ...

The url property specifies the root URL of the LoopBack API. If you do not specify a url property, the remote connector will point to it's own host name, port it's running on, etc.

The connector will generate models on the myRemoteDataSource datasource object based on the models/methods exposed from the remote service. Those models will have methods attached that are from the model's remote methods. So if the model foo exposes a remote method called bar, the connector will automatically generate the following:

app.datasources.myRemoteDataSource.models.foo.bar()

Access it in any model file

To access the remote Loopback service in a model:

module.exports = function(Message) {

  Message.test = function (cb) {
    Message.app.datasources.myRemoteDataSource.models.
      SomeModel.remoteMethodNameHere(function () {});

    cb(null, {});
  };

};

Remote data source properties

Property Type Description
host String Hostname of LoopBack application providing remote data source.
port Number Port number of LoopBack application providing remote data source.
root String Path to API root of LoopBack application providing remote data source.
url String Full URL of LoopBack application providing remote connector. Use instead of host, port, and root properties.

Configuring authentication

The remote connector does not support JSON-based configuration of the authentication credentials (see issue #3). You can use the following code as a workaround. It assumes that your data source is called "remote" and the AccessToken id is provided in the variable "token".

app.dataSources.remote.connector.remotes.auth = {
  bearer: new Buffer(token).toString('base64'),
  sendImmediately: true
};

Using with MongoDB connector

When using the MongoDB connector on the server and a remote connector on the client, use the following id property:

"id": {
  "type": "string",
  "generated": true,
  "id": true
}

changelog

2019-01-22, Version 3.4.1

  • fix: return null when findById/findOne returns 404 (Jannis Ötjengerdes)

  • style: fix linting (virkt25)

2018-06-21, Version 3.4.0

  • move dependencies (Stephen Crawford)

  • chore: drop node 4 and update deps (Taranveer Virk)

2018-01-16, Version 3.3.1

  • chore: update copyright notice years (Miroslav Bajtoš)

  • Fix duplicate definition of a remote model type (maxim.sharai)

2017-12-12, Version 3.3.0

  • Gruntfile: remove forgotten jshint task (Miroslav Bajtoš)

  • Preserve related models from "include" filter (Dimitris)

  • Add eslint to npm test, fix linter issues (Miroslav Bajtoš)

  • Refactor tests to use local per-app model registry (Miroslav Bajtoš)

2017-12-05, Version 3.2.0

  • Add support for configuring remoting options (Kenny Sabir)

  • chore:update license (Diana Lau)

  • Move remote connector doc into README (crandmck)

  • build: enable Travis CI (Miroslav Bajtoš)

  • Add stalebot configuration (Kevin Delisle)

  • Update Issue and PR Templates (#76) (Sakib Hasan)

  • Add CODEOWNER file (Diana Lau)

  • Replicate new issue_template from loopback (Siddhi Pai)

  • Replicate issue_template from loopback repo (Siddhi Pai)

  • Add "options" arg to stubbed models in tests (Miroslav Bajtoš)

2016-12-21, Version 3.1.1

  • Update package.json for LB3 release (Simon Ho)

  • Update paid support URL (Siddhi Pai)

2016-12-05, Version 3.1.0

  • Drop support for Node v0.10 and v0.12 (Miroslav Bajtoš)

  • Fix version info in README (Miroslav Bajtoš)

  • Update deps to loopback 3.0.0 RC (Miroslav Bajtoš)

2016-09-22, Version 3.0.0

  • Update deps to 3.0.0 RC (Miroslav Bajtoš)

  • Update juggler and loopback in package.json (Amir Jafarian)

  • Bump up minimal loopback version in devDeps (Miroslav Bajtoš)

2016-09-09, Version 2.0.0-alpha.2

  • Use the new API for defining remoting types (Miroslav Bajtoš)

  • Update dev-dependencies (Miroslav Bajtoš)

2016-09-01, Version 2.0.0-alpha.1

  • Update to juggler@3 and remoting@3 (Miroslav Bajtoš)

  • lib: use strict, fix whitespaces (Miroslav Bajtoš)

  • test: fix deprecation warnings (Miroslav Bajtoš)

  • test: use strict, handle errors (Miroslav Bajtoš)

  • Start development of 2.x (Miroslav Bajtoš)

2016-09-01, Version 1.3.2

  • Update README.md (Rick Blalock)

  • Update URLs in CONTRIBUTING.md (#50) (Ryan Graham)

  • Do not use native promises in tests (Simon Ho)

2016-05-06, Version 1.3.1

  • update copyright notices and license (Ryan Graham)

2016-05-03, Version 1.3.0

  • Small logic cleanup (Heath Morrison)

  • Added promise support (Heath Morrison)

  • Remove .sl-blip.js (Simon Ho)

2016-02-16, Version 1.2.0

  • Remove blip (Raymond Feng)

  • test: listen on ephemeral ports (Miroslav Bajtoš)

2016-02-04, Version 1.1.0

  • Register Models with Dynamic converter (Miroslav Bajtoš)

  • Update README.md (Simon Ho)

  • Clean up tests (Geoffroy)

  • Removed unused describe definition (Geoffroy)

  • Removed unused file (Geoffroy)

  • Removed unused dependencies (Geoffroy)

  • Add test for custom http.path configuration. (Richard Pringle)

  • test: load the correct Remote instance (Miroslav Bajtoš)

  • Refer to licenses with a link (Sam Roberts)

  • Use strongloop conventions for licensing (Sam Roberts)

  • Fix NPM license warning (Simon Ho)

  • test: specify 127.0.0.1 for test server (Ryan Graham)

  • Create proxy methods for aliases (Simon Ho)

  • Fix bad CLA URL in CONTRIBUTING.md (Ryan Graham)

  • Add include mixin (Ritchie Martori)

2014-11-27, Version 1.0.3

2014-10-10, Version 1.0.2

  • Fix support for browsers (Miroslav Bajtoš)

  • Update contribution guidelines (Ryan Graham)

2014-10-01, Version 1.0.1

  • Call resolve when defining Models (Ritchie Martori)

2014-09-26, Version 1.0.0

  • First release!