Deploying a Node.js API with Loopback to Azure Web Apps

Posted by Steven Follis on March 27, 2016

ExpressJS is a terrific starting point for creating APIs with Node.js, but can quickly difficult to manage on large, complex projects. Addressing this issue is an entire ecosystem of frameworks that has grown up around providing opinionated structure to API development, and one such framework is IBM Strongloop’s Loopback.

Loopback is a framework for developing mature RESTful API’s in the enterprise. You define a data model, autogenerate a slew of endpoints for that model, and then connect to a variety of backend data storage mechanisms. It’s terrific, especially when coming from the wide open spaces of vanilla ExpressJS.

However, by default it is a bit of a pain to deploy Loopback to the Azure App Service. Kudu, the mechanism built into Azure Web Apps that typically makes git deployments a breeze, isn’t BFF’s with Loopback.

So let’s do a quick deployment. For starters, let’s install Loopback via npm install -g strongloop. This wires up a generator that we can use to start a project. Next, we’ll use that generator to scaffold out a project with slc loopback. I chose the preset for “hello-world” so that we’ll start with a model.

Terminal Commands

When the scaffold is complete, hit node . or npm start to fire up your new API. In a browser, head over to http://localhost:3000/explorer to view the slick Explorer user interface that makes visualizing our API extremely elegant.

Explorer

Having a working API on a local machine is dandy, but we need to deploy this bad boy up to Azure. Typically we’d simply add a git remote pointing to our Azure Web App’s endpoint, as laid out here. However, when we follow those steps and deploy all we will get is a broken app. Turns out that Kudu, the management and deployment engine for Azure Web Apps, does not like Loopback’s default project structure.

To get Loopback to play nicely with Azure, we need to make one small adjustment: adding a file at the root the startup out application. To do that, create an app.js file with a few basic lines of code:

var app = require('./server/server.js')
app.start();

VSCode Screenshot

Git push that change up to your Azure Web App, navigate to /explorer and you should be good to go with a nicely scaffolded out RESTful Node.js application.

If you’re looking for the Swagger definition for use with API Apps, head over to /explorer/swagger.json and you should have the entire autogenerated schema ready for you with a bow on top.

Thanks!