<  Back to article list

Rapid API prototyping with NodeJS and Loopback

In the current state of web development and with the amount of competitiveness, especially for funding and investment that now exists, it has become extremely important to be able to quickly deliver your product, even if only as a proof of concept.

With that in mind, and with the increasing tendency to use front end libraries like AngularJS or React, the importance of having an API based backend is also becoming greater. And if the idea is to quickly develop an API server, then the Loopback framework for Node.js is an interesting option.

Loopback is a framework whose main objective is building REST APIs. It’s maintained by Strongloop (https://strongloop.com/) which is backed by IBM, and is built on top of Express and, in fact, the Strongloop company is one of the main contributors of express. It allows us to quickly expose a REST API with minimal code. It automatically creates a swagger.json file that describes your API while also providing you with an API Explorer based on that swagger.json file and where you can test your API directly on the browser by going to http://server_ip:3000/explorer.

Let’s explore this framework for a bit.

Creating an application with the Loopback cli

Even though you can create a Loopback application by hand, since the directory structure is relatively simple, there’s a cli generator that helps you do it. Just run:

npm install -g loopback-cli

You will then be able to run lb on the command line and be presented with a yeoman generator that will help you scaffold a new Loopback application.

After answering a few quick questions, you will have a working express application and, depending of your choice of answers, you can even already have authentication integrated. And that’s one of the best aspects of this framework, it comes with support for a great deal of what you’ll need for your prototype: user authentication, login through external providers (Google/Facebook/…), ACL and user/role mapping and more.

Loopback models

In loopback, the common concept of layers like you get on a typical MVC application isn’t present, and the only layer that you’ll have is the model layer. This may seem a limitation, but if we consider that we’re only exposing an API, that concept is somewhat understandable, and, with the proper adjustments, it can be expanded into a more conventional structure.
Creating models, as in most things with Loopback, can be done through the cli, in this case running the lb model command. Once more, you’ll be presented with a few questions regarding the model properties and database connection, and in the end there will be a new model in your application.

One interesting aspect about Loopback models, is that they can be attached to different data sources (MongoDB, PostgreSQL, MySQL, …), or even use other APIs as data sources. Loopback will abstract all the database layer logic from you and give you a simple API for querying data.
There’s also support for relationships between models regardless of their data source, complex data types and even model inheritance.
Model inheritance is quite interesting since it allows us, for instance, to use the built-in user model and adapt it to your own needs.

Application configuration

Another interesting feature of this framework is that it’s built with the notion of environment specific configuration. Everything, since data sources to application configuration, to enabling or disabling middleware, can be environment specific. The framework uses json files by default for the configuration, but you can also use javascript files for your environment specific overrides. It works by reading a few base json files and then, based on the NODE_ENV environment variable, detects the current environment and look for a environment specific file (either json or javascript). If the file exists it will merge both files, overriding the base settings with the more specific ones.
In the image below, we can see that there’s the middleware.json file which holds the base definition of the middleware used, and then there’s the middleware.development.json, which is the file with the overrides for the development environment.


There’s a lot more to this framework, and a lot of fine tuning that can be done on top of it to make it even better. hopefully, this article has at least sparkled your interest into learning a bit more about creating Web APIs using the Loopback framework. Also they have quite a bit of documentation available to help you learn your way.

Loopback Documentation: http://loopback.io/doc/index.html
Loopback API docs: http://apidocs.strongloop.com/

Manuel Soares

Somewhat of an errant. I've done a bunch of different stuff in the IT business, from freelancing to training to full stack web developer. From PHP to .NET to Node.js to infrastructure and architecture

Porto, Portugal https://lookatitude.com

Subscribe to Blook

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!