Smarter ideas worth writing about.

Building an API with Strongloop-Loopback Over NodeJS - Part I


I’ve been doing software development for 15 years. My current work involves potentially moving over to FeedHenry (more here) and since the CloudApp/WebAPI side is nodeJS based, I have been looking for node frameworks that would help build a RESTful API (for interaction with mobile/web apps).  The following posts are my experience going through LoopBack documentation and tutorials.

Intro to StrongLoop-Loopback:
In my research of various node frameworks (express, restify, etc.), I stumbled upon Loopback.  Some of the things I like about it are:

  • Quickly scaffold an API w/ Yeoman
  • Based off of expressJS
  • Model based (There is a CLI “wizard” to walk you though model creation)
  • Readily available connectors to a multitude of db’s such as MySQL, postGreSQL, MongoDB. (Note: Oracle, SQL Server, Sharepoint connectors do exist but are require a paid tier)
  • Built in user authentication (oAuth)
  • DB “automigration” option to initialize DB tables/collections
  • Built in “explorer” rendering a API UI on exposed methods.

While it seems there is a lot of magic going on, there is typically a manual way to create/alter existing files for better control for a developer.  In this blog post, we’ll walk though how to create a simple RESTful API that manages user reviews for gyms and health clubs.

The Setup (assuming fresh start on windows 8 OS):

  1. Install the latest version of NodeJS and NPM (instructions here).  If you have experience w/ Linux systems (like Ubuntu), NPM (Node Package Manager) would be the equivalent of apt-get to install node packages.
  2. Install strongloop (w/ loopback).  Using NPM, use the following at the command prompt.
    npm install -g strongloop
  3. (For this project) Install MongoDB (download here).  We’ll need this for our database although you are welcome to substitute this for your DB of choice.

Hello World (Creating the app):
Creating the app is actually quite simple since Strongloop utilizes Yeoman for scaffolding.

  1. First create a sub directory for the application and then create a basic loopback application by entering slc loopback at the command prompt.  This will auto generate all the initial code/files needed for a “hello world” API.
    1. a.To test and see if everything scaffolded properly, enter node . at the command prompt to start up the API.  By default the app is on port 3000 (this can be changed in \server\config.json) and if you goto http://localhost:3000 you should see some json.
    2. b.API Explorer (http://localhost:3000/explorer) – This is automatically created and is a easy way to see what REST services are open.  As you add/remove models, this will be updated for you.  You can even make test calls from here.  You will notice a User CRUD methods exposed (and more) and this comes out of the box with loopback. 

Under The Hood:

Obviously what just happened seems like “magic," but let’s go over the relevant files/folders created and the importance of those parts.

  1. The /commons/models folder holds all custom models we will be creating. We will discuss how to create models and expose them though the API in a following post.
  2. The /node_modules folder is where NPM places all dependent modules for this application.
  3. The /server folder:
    1. a.Contains configuration files (*.json) for the application.  If you need to modify the IP, port number, datasource credentials, mappings of models to datasources, etc., you’ll find it here
    2. b.The /boot subfolder holds *.js code that gets executed on startup (duh).  We will be adding a file here later on to initialize the database with dummy data.

Be sure to check back, in my next post we’ll customize our starter API using models.


About The Author

Software Developer

As a software developer, Timothy supports the App Dev team in Cardinal's Raleigh office. The majority of his experience has been with the Microsoft stack, but he has also been working on RESTful service development on current mobile initiatives.