Page Contents


The primary LoopBack JavaScript objects are:

How to get a reference to these objects depends on where the code is (in a boot script, in a model JavaScript file /common/models/model.js, and so on) as well as which object you want to reference.

Getting the app object

Getting a reference to the app object is crucial, since from it you can obtain references to other objects such as models and data sources. You’ll typically want to get a handle on the app object in:

  • Model scripts: /common/models/_modelName_.js (where modelName is the name of the model).
  • Boot scripts in /server/boot
  • Middleware (the ones you register in boot scripts and the ones in /server/server.js)

  • Your own custom scripts

The app object provides context into various parts of a typical LB app.

From a boot script 

To get a reference to the app object in a boot script, pass it as the first parameter in the exported function.

Asynchronous boot script with a callback function:

Asynchronous boot script - /server/boot/your-script.js

module.exports = function(app, cb) { //app is injected by LoopBack

Synchronous boot script without a callback function:

Synchronous boot script - /server/boot/your-script.js

module.exports = function(app) { //app is injected by loopback

As you can see from both examples, LoopBack provides the app object automatically as the first parameter in your boot scripts.

See Defining boot scripts for more information about boot scripts.

From middleware

LoopBack sets app object automatically in the request object in middleware (actually, under the hood, Express does it). You can access in server/server.js as follows:

Middleware - /server/server.js

app.use(function(req, res, next) {
  var app =;

See Defining middleware for more information on middleware.

From a custom script

If you need a reference to app in your own custom scripts, simply require it (as in the models example):

A custom script - /server/your-script.js

var app = require('/server/server');

You simply require /server/server.js as you would any Node module.

From a model script

To get a handle on the app object in a model scaffolded by the Model generator, “require” it as you would any Node module:

Model - /common/models/book.js

var app = require('../../server/server'); //require `server.js` as in any node.js app

module.exports = function(Book) {