Module: loopback

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.

Arguments
Name Type Description
name String

Name of the connector, e.g. 'mysql'.

connector Object

Connector object as returned by require('loopback-connector-{name}').

app.dataSource(name, config)

Define a DataSource.

Arguments
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.

Arguments
Name Type Description
[cb] Function

If specified, the callback is added as a listener for the server's "listening" event.

Returns
Name Type Description
result http.Server

A node http.Server with this application configured as the request handler.

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' });
Arguments
Name Type Description
Model Object or String

The model to attach.

config Object

The model's configuration.

config
Name Type Description
dataSource String or DataSource

The DataSource to which to attach the model.

[public] Boolean

Whether the model should be exposed via REST API.

[relations] Object

Relations to add/update.

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

  1. Call app.models() to get a list of all models.
var models = app.models();

models.forEach(function(Model) {
 console.log(Model.modelName); // color
});
  1. 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;
Returns
Name Type Description
result Array

Array of model classes.

app.remoteObjects()

Get all remote objects.

Returns
Name Type Description
result Object

Remote objects.

app.remotes()

Lazily load a set of remote objects.

NOTE: Calling app.remotes() more than once returns only a single set of remote objects.

Returns
Name Type Description
result RemoteObjects