20 Jan

Getting Started with StrongLoop

What it StrongLoop

The StrongLoop API is a platform featuring the LoopBack framework – an open-source node framework which enables easy creation of REST APIs and the connection to backend data sources such as MySQL or MongoDB databases. In this Blog Post I will show how I got started with the framework and how I used it to created a simple web application.

Getting started with a small example app

For getting started with my first project, I followed the guide at http://strongloop.com/get-started/. Creating a StrongLoop project is easily done by calling the loopBack application generator with the command $ slc loopback. The generator will prompt you for a name and directory of your application and then generate a scaffolding StrongLoop project.

Setting up the StrongLoop project

The loopBack application generator is one of many slc command line tools, some of which we will encounter later on. Alternatively, StrongLoop also provides a graphical UI – called ARC – which can be used to execute the same tasks by button clicks, but personally, I was more comfortable with the command line. I liked that it leads you through the processes step by step by prompting you for one thing at a time.

1. Generating a scaffolding project

We will now have a closer look at the structure and components of the project we have just created:

File Structure of the StrongLoop Project

The project distinguishes between the client and the server parts of the application. We will find the client folder to be empty – this is where we will later place our application’s front end files.
The server folder contains the default configurations, the default boot scripts for basic initialization (they will be run when the server starts) and the server.js wich will start the web server if we run the application. The database.json lists a default in-memory data source named db. I added a file name so that all the data we generate with the app (such as users, bookings, …) will be written into that file:

Later on, we could add a real database here. For now, however, I want to focus on getting to know the basic ideas of StrongLoop, so a simple json file as data storage will be sufficient as a start.

Apart from that, our application already contains several built-in models (in the node_modules/common/models folder). Models represent backend data sources and always are created as a json file specifying the object parameters and an accompanying JavaScript file in which we can define further methods. The built-in models are models which are likely to be used in most applications, such as User, and thus save us time and effort to create them ourselves.

LoopBack also provides a predefined REST API for each model, comprising the CRUD operations (create, read, update, delete) – so again we are saved the trouble of creating this basic functionality by ourselves!

2. Experimenting with User REST Endpoints

Seeing that our application already has several models and REST endpoints, we can start exploring them in order to learn how they can be used. In order to do that, we need to start the server ($ slc run) and go to http://localhost:3000/explorer/, where we will find a list of the existing REST endpoints. By default, only the User model is exposed via the REST API, so we should not be surprised not to find the other built-in models there.The UI allows us to try out the different endpoints. We can, for example, click on POST /Users and create a new instance of a user by specifying a username, password, etc. The UI then displays the URL of the request we have just made, as well as its response code and response message. I found this “playground” very helpful for learning in what way to use these endpoints later in the actual application.

3. Adding custom models and custom REST APIs

So far, we have only talked about the application’s default elements and functionalities. It is now time to expand the app by our own custom elements. For defining custom models, we can use the LoopBack model creator ($ slc loopback:model,) which takes us step by step through the setup of a model. Since I wanted my first StrongLoop app to be a small programme for making holiday reservations, I named my first model ‘Booking’ and chose fitting parameters:

Creating the Booking Model with the LoopBack model creator

This is the resulting booking.json which we find in the newly generated folder common/models. All custom models are placed into this folder automatically:

Resulting Booking Model

The booking.js file does not contain any code at this point apart from the declaration of the class. However, we also want to define our own custom REST Endpoints, so I added a remote method, which is a static method of a model that is exposed over the REST API. My method is called ‘welcome’ and simply creates a welcoming message. If we run the app again, we see that http://localhost:3000/explorer/ now also includes endpoints for our custom Booking model – the default CRUD operations as well as the custom endpoint GET /Bookings/welcome. If we try out the new endpoint, we get the following result:

Response to Bookings/Welcome

4. Integrating a client app

Having gotten the basic grip of StrongLoop and how to use and create REST Endpoints, I integrated my client app – an application for making holiday reservations. I use the REST Endpoints in my client app in order to login and register users, to retrieve information from the server and to create new reservations. The good thing about StrongLoop is that it leaves it up to the programmer to decide which languages and technologies to use for the client app, so you are free to chose to use Android, iOS or, as I did, the Angular.js framework.

What I liked about StrongLoop

I think that StrongLoop is a great framework for the creation of web applications because it is easy to learn and straightforward to use. It creates many useful default elements such as the user model, while at the same time maintaining a clear project structure. And I particularly like that you can view and try out each of the project’s REST APIs, which is especially helpful if you are new to the subject of using REST APIs.

The next steps for me will be to delve into more advanced features such as user authentication and the inclusion of real databases.

Share this

Leave a reply