Introduction to loopback

odejs framework: Loopback is open source node.js framework build on top of the express framework. IBM and strong loop community took responsibility to maintain loopback project. Please note that this is just an introductory post, I will be explaining each and every feature listed here in detail.


  1. We can create API really quickly
  2. We can connect to multiple data source
  3. Role-based authentication with no or minimal code
  4. Swagger application to document API


One should have a basic understanding of express to understand this post in a better way.

Setting up the environment

The easiest way to create a loopback application is to use Application generator (like yoman). It is possible to create a LoopBack application by coding it from scratch, but the application generator does all the “heavy lifting” to create the basic scaffolding of the standard project layout. You can then customize the application to suit your needs using the CLI tools. To create a basic loopback application run command as shown below:

$ npm install -g loopback-cli

You should have nodejs and npm pre installed to run above command.

A key feature of LoopBack is that when you define a model it automatically generates predefined REST API with a full set of CRUD operations. 


Models are the heart of loopback. A model contains JSON and js file by default. So, let's discuss more about models and see some types of model that we can create in loopback application. Basically, we can categorize model into three types. These are:

  1. Connected Model
  2. Built-in Models
  3. Custom Models

Connected Model

When we attach the model to persistence data source it becomes a connected model. We can connect model to a data source with help of connectors.

Built-in models

Every LoopBack application has a set of predefined built-in models such as User, Role, and Application, so you don’t have to create these common models from scratch.

Custom models

You can define your own custom models specific to your application. Using command

lb model

we can generate a model.

When we generate a model, it automatically created a default unique field as `id` and if model is connected to persistance data source this id is known as primary key.

Model relations

You can express relationships between models, such as BelongsTo,  HasMany, and  HasAndBelongsToMany. We can create relations between models using `lb relation`. I will be discussing more about it in later posts.

Application logic

You can add custom application logic in several ways:

  1. Add logic to model javaScript file
  2. Add logic to boot folder by creating a js file
  3. Defining custom middleware like express application

You can add code to validate data before saving it to the model and back-end data store.

Middleware in loopback

Middleware in loopback is similar to express but it overcomes the issue of the order of execution that we have in express. It adds a concept of phases to overcome this issue.

LoopBack components

LoopBack components provide additional “plug-in” functionality. Some of the components that loopback supports are as listed below:

  1. Push notifications -  enables sending information to mobile apps for immediate display in a ”badge,” alert, or pop-up message on the mobile device.
  2. Storage component - enables uploading and downloading files to and from cloud storage providers (Amazon, Rackspace, OpenStack, and Azure) as well as the server file system.
  3. Third-party login - integrates Passport and enables user login (and account linking) using third-party credentials from Facebook, Google, Twitter, Github, or any system that supports OAuth, OAuth 2, or OpenID.
  4. Synchronization - enables mobile applications to operate offline and then synchronize data with the server application when reconnected.
  5. OAuth 2.0 -  enables LoopBack applications to function as oAuth 2.0 providers to authenticate and authorize client applications and users to access protected API endpoints.


As loopback is built on express so routing in loopback is similar to that of express. Though, loopback routing depends on loopback middleware chain.The middleware to be loaded during the middleware phases are configured in the `middleware.json` file of the app.

Define access controls

Access controls generally give us control over the accessibility of the model. Using access control we can actually restrict our model APIs. For example: If we want that end user should not be able to create new users we can define access control for it.We can define access controls using command:

lb acl

Introducing remote hooks

A remote hook is simply a function that gets executed before or after a remote method (either a custom remote method or a built-in CRUD method).

You can define two kinds of remote hooks:

  • beforeRemote() runs before the remote method.

  • afterRemote() runs after the remote method.

Both of the remote methods take two function arguments: a method name and a callback function.

For e.g:

Modeltest.beforeRemote('create', function() { })

Introducing the AngularJS SDK

LoopBack provides an AngularJS JavaScript SDK to facilitate creating AngularJS clients for your LoopBack API server-side apps. To generate the Angular services for a LoopBack application, use the AngularJS SDK lb-ng command-line tool.

$ npm install -g loopback-sdk-angular-cli

Loopback bootstrapping

LoopBack will always load the following configuration files, if they exist:

  • server/config.json.

  • server/config.local.json or server/config.local.js. 

  • server/datasources.json

  • server/datasources.local.json or server/datasources.local.js

  • server/model-config.json

  • server/model-config.local.json or server/model-config.local.js

  • server/middleware.json

  • server/middleware.local.json or server/middleware.local.js

  • server/component-config.json

  • server/component-config.local.json or server/component-config.local.js

We can write our configuration in JS files as well if it's not possible to write in the json file. But make sure that JSON file should be in placed at right position.

Additionally, when the NODE_ENV environment variable is set, LoopBack will load configuration from:

  • server/config.env.json/js

  • server/datasources.env.json/js

  • server/model-config.env.json/js

  • server/middleware.env.json/js

  • server/component-config.env.json/js

A LoopBack application can load multiple configuration files, that can potentially conflict with each other. The value set by the file with the highest priority will always take effect. The priorities are:

  1. Environment-specific configuration, based on the value of NODE_ENV; for example, server/config.staging.json.

  2. Local configuration file; for example, server/config.local.json.

  3. Default configuration file; for example, server/config.json.

I will be sharing more about loopback in the upcoming posts. Please let me know if you have any questions in commment section below.

Author Info

Tavish Aggarwal


Tavish Aggarwal is a front-end Developer working in a Hyderabad. He is very passionate about technology and loves to work in a team.