Applications communicate with each other through what is called an Application Programming Interface (API). Wikipedia defines an API as – “In computer programming, an application programming interface (API) is a set of subroutine definitions, protocols, and tools for building application software. In general terms, it is a set of clearly defined methods of communication between various software components.”
Today, it’s almost impossible to imagine an enterprise or client facing application without API capabilities. Therefore, it is imperative that during the application development lifecycle, developers can quickly create and deploy APIs as part of the overall application architecture. In this blog, we will highlight two frameworks – Node.js and LoopBack, when used together, help developers achieve rapid API development.
Features of LoopBack:
Let’s look at the features of LoopBack that make it a popular framework-
- Create Dynamic REST (Representational State Transfer) APIs: REST APIs can be created in Loopback within minutes with little or no coding.
- Connects with multiple data sources: You can connect multiple data sources like MySQL, MongoDB, PostgreSQL and more within a single LoopBack application. Connectors specific to these data sources are available.
- Provides Access Control for Complex APIs: LoopBack ACL provides built-in, role-based, on-access controls for an endpoint. There is no need for code to be written from scratch for JWT authentication.
- Separate components for file storage, third-party logins and OAuth 2.0: LoopBack component storage allows uploading files to the local filesystem, AWS S3 and other popular storage options. Loopback component passport allows users to login using their existing accounts on Facebook, Google, twitter and others.
- Default API Explorer: LoopBack comes integrated with Swagger UI which provides API explorer for your APIs.
- LoopBack Command Line Interface: LoopBack comes with command line tool for scaffolding a LoopBack application.
Creating a simple API using LoopBack:
Let’s look at the steps to create a simple API using LoopBack.
The following command is used to install LoopBack Command Line which scaffolds a new LoopBack application and install LoopBack as a dependency through the lb command.
This will install the lb command globally. Make sure that Node.js is installed prior to executing this command.
The following command is then executed to create a project:
After this, a prompt will be displayed to enter the name of your application and the directory to hold the project. Here, the name entered is loopback-api.
The version 3.x of LoopBack is selected (the current stable version). In the next step, it prompts to select the type of application that is intended to be created. Below are the four options from where the type can be chosen:
Let’s choose to install the hello-world application. After the selecting the type of application, the required dependencies will be installed.
The following commands are entered to start the application:
Navigating to http://localhost:3000/explorer will open up the API explorer.
The Anatomy of a LoopBack application:
To create dynamic APIs using LoopBack, it is important to understand the directory structure of the application.
- ServerThis directory holds the server-side files of the application.
- server/boot:This directory holds the initialization scripts required for the application. The scripts are executed in the alphabetical order.
- Component-config.json: The component-config.json file specifies the components that need to be enabled/disabled for your application. For e.g. the API explorer can be disabled by disabling the loopback-component-explorer, when the application is deployed to production.
- Config.json:Config.json file is used to configure your application settings like port to use and your API path.
- Datasources.json:Datasources.json file is used to configure all the data sources which are consumed by the application. Data sources can be a database or an object storage component like AWS S3/Cloudinary. Configurations required to setup these data sources are specified in this file.
- Middleware.json:Middleware.json file is used to hold all the express middleware used by the application. Customized middleware can also be created.
- Model-config.json: Model-config.json file is used to configure the settings for the active models. These settings include location of the models and routes to be enabled/disabled.
- Server.js: This file is the entry point of the application and is used to bootstrap your LoopBack application.
As mentioned previously, LoopBack v3 is the current stable version, but it does not implement any design pattern. Models are solely responsible for exposing APIs as well as for data access. LoopBack v4 leverages TypeScript for better code quality. It is not yet released- only available as a developer preview.
LoopBack is the one of the most flexible frameworks helping application development team to deliver quality releases. LoopBack is maintained by IBM StrongLoop and has garnered community support from google groups, stack overflow and gitter. It has become the de-facto choice for complex applications as many start-ups and big enterprises have embraced it for the design, execution and testing of their high-performance APIs. Contact us if you would like to utilize LoopBack with Node.js to accelerate development.