Rapid API development using LoopBack and Node.js

By –

Emtec Digital Think Tank

July 3, 2018

Share –

Rapid API development using LoopBack and Nodejs

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.

Node.js is a lightweight, open source, cross platform JavaScript environment which can be used in conjunction with a framework like LoopBack for rapid API development. Loopback is a highly extensive open source Node.js framework for creating APIs with relatively less coding. LoopBack facilitates easier generation of APIs, database connections and more, allowing developers to solely focus on solution architecture.

Features of LoopBack:

Let’s look at the features of LoopBack that make it a popular framework-

  1. Create Dynamic REST (Representational State Transfer) APIs: REST APIs can be created in Loopback within minutes with little or no coding.
  2. 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.
  3. 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.
  4. 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.
  5. Default API Explorer: LoopBack comes integrated with Swagger UI which provides API explorer for your APIs.
  6. 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.

npm install -g loopback-cli

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:

lb

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.

? What’s the name of your application: () loopback-api

? Enter name of the directory to contain the project: (loopback-api)

 

create loopback-api/

 

info change the working directory to loopback-api

? Which version of LoopBack would you like to use? (Use arrow keys)

2.x

3.x

? What kind of application do you have in mind? (Use arrow keys)

> api-server (A LoopBack API server with local User auth)

empty-server (An empty LoopBack API, without any configured models or datasources)

hello-world (A project containing a controller, including a single vanilla Message and a single remote method)

notes (A project containing a basic working example, including a memory database)

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:

  1. API-server
  2. empty-server
  3. hello-world
  4. notes

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:

cd loopback-api

node .

 

Web server listening at: http://localhost:3000

Browse your REST API at http://localhost:3000/explorer

Navigating to http://localhost:3000/explorer will open up the API explorer.

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

Loopback Application

  • ClientClient directory will hold JavaScript, HTML and CSS files of the frontend application. AngularJS, IOS and Android SDK can be used to create the client-side 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.

Conclusion

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.

Subscribe for Latest Content

Want to scale your organizational digital initiatives?

Sign up for insights