A single code base application that contains your server side rendering, API (REST or otherwise), business logic, client side application and testing frameworks is nice when your application is small or you’re showing a demo at a conference. However in the real world your application is likely much larger and intricate than that of a demo made in 5 minutes.
The Strongloop team has provided examples as well as a command line application scaffolding utility to get you up and running quickly. While this scaffold-ed application is full featured and great for getting started. You are still left with an application that was created in 5 minutes. It will need a little thought and work to get it into a real world application.
Separating out the pieces
The common project will contain all your:
- common business logic
- common utilities.
In Loopback your API is alot of configuration and convention ontop of your models. Spliting this out into it’s own project allows for:
- Scale independently
- Simplify your code base
- Streamline middleware
Your site is comprised of your server side rendering and client side rendering. It’s responsible for:
- Server side routing
- Views & Controllers
- Client side application (AngularJS or whatever suits your fancy)
The key to making this work is identifying what is required in each project in order to operate and function together.
The primary points to focus on are:
- Node modules
- boot scripts
- exporting module singleton objects (loopback & configuration)
- model locations (specifically when utilizing loopback-component-passport)
Simply clone the API and SITE projects into their own directory. Then you can run
node . in the api and site directories or create a combination script to start up both
var api = require('./api').loopback; var site = require('./site'); api.start(); site.start();