LoopBack Example Application
You must have already installed Node.js.
$ npm install -g strongloop
If you run into any issues, see Installing StrongLoop for more information.
i-Car is an (imaginary) car rental dealer with locations in major cities around the world. They need to replace their existing desktop reservation system with a new mobile app.
This application was created using LoopBack tools. See Building from scratch below for more details.
Start the application back-end by running the following command:
$ node .
Now open your browser and point it to http://127.0.0.1:3000 to access the application UI.
The app enables customers to find the closest available cars using the i-Car app on a smartphone. The app shows a map of nearby rental locations and lists available cars in the area shown on the map. In addition, the customer can filter the list of cars by make, model, class, year and color. The customer can then select the desired car and reserve it via the app. If not logged in the app prompts the customer to login or register. The app indicates if the desired car is available and if so, confirms the reservation.
/carsexposes a queryable (filter, sort) collection of available cars over HTTP / JSON
/cars/nearby?&lat=...&long=... or ?zip=...returns a filtered set of available cars nearby the requesting user
/cars/nearby?id=24&zip=94555returns nearby cars of id 24.
/cars/:idreturns a specific car from the inventory, with specific pricing and images
/users/loginallows a customer to login
/users/logoutallows a customer to logout
By default, the sample application uses the memory connector and listens on the port 3000 on all network interfaces.
You can configure other data sources by adding a new key to
var DATASTORES =custom:connector: 'my-custom-connector'// configuration for the custom connectormemory:// etc.;
The sample can be configured using the following environment variables:
DB: The db type, use
IP: The http server listener ip address or hostname, default to
PORT: The http server listener port number, default to
To run the application at port 3001 with MongoDB:
$ DB=mongodb PORT=3001 node .
To run the application at port 3002 with Oracle:
$ DB=oracle PORT=3002 node .
The app will get all customer information from the SalesForce API.
All car inventory is already available in an existing Oracle X3-8 Exadata database.
The Inventory DB schema looks like this:
View to return qty of available products for the given city.
Google's location API is used to return the users city from a given zip or lat/long.
The project is composed from multiple components.
models/ contains definition of models and implementation of custom model
rest/ contains the REST API server, it exposes the shared models
via REST API.
website/ contains a simple single-page-application that is served
when users open the project in the browser.
server/ is the main HTTP server that brings together all other components.
sample-data/ contains a set of sample models that are used to initialize
the database with some data.
test/ provides few basic unit-tests to verify that the server provides
the expected API.
Refer to Creating an application for more information.
You can create most of the sample application using LoopBack's Yeoman generators.
slc loopback command-line reference
for more information.
Install StrongLoop with
npm install -g strongloop to install the LoopBack generators.
Once you have the generators installed, run the following command to recreate the sample app from scratch:
$ slc loopback:example example-app -l
This will call other generators like
slc loopback and
to scaffold the application. You can learn more about these generators in the
slc command-line reference.
When run with the
-l option, the example generator
prints a detailed list of steps that are executed to walk you trough the
scaffolding process. You can re-run the steps manually yourself to get a better
understanding of how the loopback generators work.
This is how the output looks like:
Create initial project scaffolding$ slc loopback:app loopback-example[?] Enter a directory name where to create the project: .[?] What's the name of your application? loopback-exampleI'm all done. Just run npm install to install the required dependencies.Add datasource geo$ slc loopback:datasource geo[?] Select the connector for geo: restSet datasource options: operationsAdd model Car$ slc loopback:model Car[?] Select the data-source to attach Car to: db[?] Expose Car via the REST API?[?] Property name:$ slc loopback:property[?] Select the model: Car[?] Enter the property name: id[?] Property type: string[?] Required? falseSet property options: id(more properties follow in the output)Add relation Car hasMany ReservationSet model options: mysql mongodb oracleAdd model Customer(and so on)
The first step is to prepare the project infrastructure by
slc loopback:app, which is an alias for
Then there is a
geo datasource added by running
slc loopback:datasource geo.
When prompted for the connector to use, the
rest is selected. After the
datasource was created, the REST operations are added to the generated
The next step is to create all application models. The generator
slc loopback:model is called to define a new model,
slc loopback:property to
add a property definition to the new model. Some property options like
"id": true are not supported by the generator, they are added manually.
Also model relations and per-datasource model configurations are not supported by the generator yet, as can be seen from these two lines:
Add relation Car hasMany ReservationSet model options: mysql mongodb oracle
Open the file
models/car.json too see what has been added by the generator.
When all models are defined, the example generator performs steps that you would do manually when working on a new application: add more dependencies to package.json, extend the models with custom behaviour, implement unit-tests, etc.