a blog for those who code

Monday 15 June 2015

LoopBack - NodeJS API Framework

In this post we are going through LoopBack - Node API Framework. It is a powerful Node.js framework for creating APIs and easily connects to the backend data sources. It is built on top of Express and it can easily generate REST API that can be called by any client.


PC : http://loopback.io/

LoopBack is a highly extensible and an open-source Node.js framework. You will even have an option to to use StrongLoop Arc to visually edit, deploy, monitor LoopBack apps. StrongLoop Arc is a graphical UI for the StrongLoop API framework that complements the slc command line tools for developing APIs quickly and getting them connected to the data.

Getting Started with LoopBack

Install the StrongLoop command line tools for LoopBack applications through npm

$ npm install -g strongloop

Then we will run loopback application generator using $ slc loopback. It will ask us to create a LoopBack application as shown below.


After giving the name of the application as example, it will install all the required dependencies and it will tell us what all are the next steps. So as you can see in the below image that the next steps are Create a model in your app, Optional : Enable StrongOps monitoring and then run the app.


Now lets add a model, our first model will be Planets. Loopback will be asked for data-source and base class. We have used db (memory) as the data-source and PersistedModel as the Model's base class. Then LoopBack will asks that the Model (Planets) should be exposed via Rest API ( yes in our case) and the name of the Rest Service (ex in our case). Next loopback will asks to give the name of the properties, their data types and required/non-required flags as shown in the below diagram.


You can use the loopback command to create and scaffold application. The slc loopback command provides and Application Generator to create new LoopBack application and a number of sub generators to scaffold an application as described below.

slc loopback:example - Creates the LoopBack example app
slc loopback - Creates a new LoopBack application
slc loopback:datasource - Add a new data-source to a LoopBack application
slc loopback:model - Add a new model to a LoopBack application
slc loopback:property - Add a new property to an existing model
slc loopback:acl - Add a new access control list (ACL) entry to the LoopBack application
slc loopback:boot-script - Add a new boot-scripts
slc loopback:swagger - Generates a fully functional application that provides the APIs conforming to the Swagger 2.0 specification.

Now Run the application with $ node . ,in your browser go to http://localhost:3000/explorer. You can see the your entities with list of operations available as shown below.


LoopBack can save you a lot of time by reducing the number of manual steps one need to do in creating restful API. Please Like and Share the Blog, if you find it interesting and helpful.

No comments:

Post a Comment