LoopBack 3.x -The most comfortable framework for creating Node.js REST APIs

I had tested a few frameworks to develop a perfect REST APIs for my music streaming server, including Express.js and Restify.js. But I am not here to compare those Node.js frameworks by comparing their advantages and disadvantages. You can find a load of comparison between those Node.js frameworks on Internet. So, let's find out how we gonna use Loopback to create a robust Node.js REST APIs.

This is the self-referencing note for myself and I m just adding note to my journal. If you want to read the official API Docs, you can find at http://loopback.io/doc/en/lb2/index.html.

Only one thing cannot be the best for everyone. Everybody has their own preferences and choices. For Node.js REST APIs, I choose Loopback.

The LoopBack framework

It is no more than a set of Node.js modules that you can quickly build REST APIS, which interact with data sources (memory or db) through the LoopBack model API. These exposed APIs will be available to access from other modules locally with the app or remotely over REST, and via native client APIs for Android, iOS and HTML5. Besides, all important functions such as querying databases, storing data, sending emails, push notifications, registering users and uploading files, are also inclusively built in the framework.

Key LoopBack modules, their relation and dependencies

We will skip those key components as it is clearly seen in the above diagram, which you can also see at Official Docs. For now, let's take a look how Loopback works.

How LoopBack works - Models

Everything in LoopBack originate at Models. They are JavaScript objects and works as back-end data sources such as databases.
A Basic model object has methods of adding hooks and validating data. Any model objects created will all "inherit from" it. When a model is attached to a persistent data source, the inheritance hierarchy of LoopBack models makes it a connected model with built-in create, retrieve, update and delete operations.

Hooks allow you to add a custom logic before or after a specific event in the model's lifecycle.

Built-in models

You don't have to create User, Role and Application models as they have been predefined as built-in models in every LoopBack application.

Custom models

If you need more than built-in models, you can define your own custom models by:
1. Using the LoopBack model generator.
2. Using model discovery based on an existing relational database.
3. Using instance introspection on free form data in NoSQL databases.

For example: There can be more than one type of users in every application such as Customers, Administrators and Subscribers. All of these users models should be integrated with authentication and authorisation operations. Then, you just need to tell LoopBack that you will create a custom model named, Customers based on built-in Users model. All of the predefined Users methods such as login, logout, forgot password and roles definition will be automatically available to our "Customers" model.

All of the models are defined in each JSON file that contains information about name, type, a boolean for idInjection, a set of properties, validation rules, model relations, access control lists and the methods of the related model. You can edit the model definition JSON file manually anytime.

I know you are now very keen to try LoopBack framework, or already tried on your terminal, and start coding to find out the these awesome features. Trust me, nothing comes out form a little knowledge but a terrible mess. I had been down to that road. When the "Coder" mode is on, it is a bit difficult to turn it off. Finally, the result is becoming a "Cheater", who just copy and paste a block of codes with no clues what it does.

Model relations

Let's continue to Model relations. In Loopback, you can express relationships between models such as BelongTo, HasOne. HasMany and etc.
For example:
An artist has one or more genres and a genre can have zero or more genres.

How LoopBack works - Application Logic

Without LoopBack framework, every methods taken by the application will be named as application logic by our own. However, with LoopBack the fundamental logic for an basic REST APIs is already done. Besides those, you can add your own application logic directly to models, through the CRUD operations of the models, by adding boot scripts and by defining custom middleware.

How LoopBack works - Data sources and connectors

Backend services such as Databases, REST APIS and storage services are termed as data sources in LoopBack that are backed by connectors. And connectors are used in order to access data sources by application through DataSource and PersistedModel APIs.

How LoopBack works - Components

With LoopBack components, you can add additional functionalities without adding new works to your development such as in cases of:
1 Push notifications
2 Storage component
3 Third-party login (Passport)
4 Synchronisation
5 OAuth 2.0.....

Thanks you for taking your time to read this post and I do not hope that you get much joy from this post. In up coming posts, we will start creating a simple Node.js API that do basic user login and logout functions by using LoopBack framework.

Nay Win Myint

Founder and CEO of Pancasikha Music Streaming Provider, JavaScript full-stack and Android developer and Graphic designer.

Rangoon, Myanmar