The remote connector enables you to use a LoopBack application as a data source via REST.Note: This page was generated from the loopback-connector-remote/README.md.

See also:

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:

  1. $ 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:

  1. $ 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:

/server/datasources.json

  1. ...
  2. "myRemoteDataSource": {
  3. "name": "myRemoteDataSource",
  4. "connector": "remote",
  5. "url": "http://localhost:3000/api"
  6. }
  7. ...

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 arefrom 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:

  1. module.exports = function(Message) {
  2. Message.test = function (cb) {
  3. Message.app.datasources.myRemoteDataSource.models.
  4. SomeModel.remoteMethodNameHere(function () {});
  5. cb(null, {});
  6. };
  7. };

Remote data source properties

PropertyTypeDescription
hostStringHostname of LoopBack application providing remote data source.
portNumberPort number of LoopBack application providing remote data source.
rootStringPath to API root of LoopBack application providing remote data source.
urlStringFull 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”.

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

Using with MongoDB connector

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

  1. "id": {
  2. "type": "string",
  3. "generated": true,
  4. "id": true
  5. }

Tags: connectors