See also: Example application
The remote connector enables you to use a LoopBack application as a data source via REST. The client can be a LoopBack application, a Node application, or a browser-based application running 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.
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 an remote data source
Create a new remote data source with the datasource generator:
$ apic create --type datasource
$ slc loopback: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:
...
"myRemoteDataSource": {
"name": "myRemoteDataSource",
"connector": "remote",
"url": "http://localhost:3000/api"
}
...
The url
property specifies the root URL of the LoopBack API.
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, the following id property should be used.
"id": {"type": "string", "generated": true, "id": true}