var app = loopback()
LoopBackApplication
The App
object represents a Loopback application.
The App object extends Express and supports Express middleware. See Express documentation for details.
var loopback = require('loopback');
var app = loopback();
app.get('/', function(req, res){
res.send('hello world');
});
app.listen(3000);
app.connector(name, connector)
Register a connector.
When a new data-source is being added via app.dataSource
, the connector
name is looked up in the registered connectors first.
Connectors are required to be explicitly registered only for applications using browserify, because browserify does not support dynamic require, which is used by LoopBack to automatically load the connector module.
Name | Type | Description |
---|---|---|
name |
String
|
Name of the connector, e.g. 'mysql'. |
connector |
Object
|
Connector object as returned by |
app.dataSource(name, config)
Define a DataSource.
Name | Type | Description |
---|---|---|
name |
String
|
The data source name |
config |
Object
|
The data source config |
app.enableAuth()
Enable app wide authentication.
app.listen([cb])
Listen for connections and update the configured port.
When there are no parameters or there is only one callback parameter,
the server will listen on app.get('host')
and app.get('port')
.
For example, to listen on host/port configured in app config:
app.listen();
Otherwise all arguments are forwarded to http.Server.listen
.
For example, to listen on the specified port and all hosts, and ignore app config.
app.listen(80);
The function also installs a listening
callback that calls
app.set('port')
with the value returned by server.address().port
.
This way the port param contains always the real port number, even when
listen was called with port number 0.
Name | Type | Description |
---|---|---|
[cb] |
Function
|
If specified, the callback is added as a listener for the server's "listening" event. |
Name | Type | Description |
---|---|---|
result |
http.Server
|
A node |
app.model(Model, config)
Attach a model to the app. The Model
will be available on the
app.models
object.
Example - Attach an existing model:
var User = loopback.User;
app.model(User);
Example - Attach an existing model, alter some aspects of the model:
var User = loopback.User;
app.model(User, { dataSource: 'db' });
Name | Type | Description |
---|---|---|
Model |
Object or String
|
The model to attach. |
config |
Object
|
The model's configuration. |
Name | Type | Description |
---|---|---|
dataSource |
String or DataSource
|
The |
[public] |
Boolean
|
Whether the model should be exposed via REST API. |
[relations] |
Object
|
Relations to add/update. |
Name | Type | Description |
---|---|---|
result |
ModelConstructor
|
the model class |
app.models()
Get the models exported by the app. Returns only models defined using app.model()
There are two ways to access models:
- Call
app.models()
to get a list of all models.
var models = app.models();
models.forEach(function(Model) {
console.log(Model.modelName); // color
});
- Use
app.model
to access a model by name.app.models
has properties for all defined models.
The following example illustrates accessing the Product
and CustomerReceipt
models
using the models
object.
var loopback = require('loopback');
var app = loopback();
app.boot({
dataSources: {
db: {connector: 'memory'}
}
});
app.model('product', {dataSource: 'db'});
app.model('customer-receipt', {dataSource: 'db'});
// available based on the given name
var Product = app.models.Product;
// also available as camelCase
var product = app.models.product;
// multi-word models are avaiable as pascal cased
var CustomerReceipt = app.models.CustomerReceipt;
// also available as camelCase
var customerReceipt = app.models.customerReceipt;
Name | Type | Description |
---|---|---|
result |
Array
|
Array of model classes. |
app.remoteObjects()
Get all remote objects.
Name | Type | Description |
---|---|---|
result |
Object
|
app.remotes()
Lazily load a set of remote objects.
NOTE: Calling app.remotes()
more than once returns only a single set of remote objects.
Name | Type | Description |
---|---|---|
result |
RemoteObjects
|