In this workshop, you will learn how to build a full-stack Whiskey-voting application using LoopBack and AngularJS.
Table of Contents generated with DocToc
- Scaffold an API server in 5 minutes
- Explore the API
- Import sample data
- Add an AngularJS frontend
- Generate Angular client services
- Wire up the controllers and the REST API
- Celebrate!
- What to do next
- Start by installing LoopBack's Yeoman generator:
$ npm install -g generator-loopback
- Create and switch to your project's directory:
$ mkdir whiskey
$ cd whiskey
- Scaffold a loopback application:
$ yo loopback
- Add a "Whiskey" model with the properties "name" (string), "distillery" (string), "imageUrl" (string):
$ yo loopback:model Whiskey
[?] Enter the model name: Whiskey
[?] Select the data-source to attach Whiskey to: db (memory)
[?] Expose Whiskey via the REST API? Yes
[?] Custom plural form (used to build REST URL):
Let's add some Whiskey properties now.
Enter an empty property name when done.
[?] Property name: name
invoke loopback:property
[?] Property type: string
[?] Required? Yes
Let's add another Whiskey property.
Enter an empty property name when done.
[?] Property name: distillery
invoke loopback:property
[?] Property type: string
[?] Required? Yes
Let's add another Whiskey property.
Enter an empty property name when done.
[?] Property name: imageUrl
invoke loopback:property
[?] Property type: string
[?] Required? Yes
Let's add another Whiskey property.
Enter an empty property name when done.
[?] Property name:
-
Add a "Review" model with the properties "rating" (number) and "comment" (string).
-
Setup the relation a "Whiskey" has many "reviews":
yo loopback:relation
[?] Select the model to create the relationship from: Whiskey
[?] Relation type: has many
[?] Choose a model to create a relationship with: Review
[?] Enter the property name for the relation: reviews
[?] Optionally enter a custom foreign key:
- Start your API server
$ node .
- Open LoopBack Explorer in your favourite browser:
$ open http://localhost:3000/explorer
- Create a Whiskey entry using the Explorer:
POST /Whiskeys
{
"name": "Green Spot 12-year-old",
"distillery": "Midleton",
"imageUrl":
"http://static.whiskybase.com/storage/whiskies/5/3/084/86415-big.jpg"
}
Write down the generated id
(e.g. 1
).
- Create a Rating entry using the explorer, use the
id
generated in the previous step as{id}
parameter in the route:
POST /Whiskeys/{id}/reviews
{
"rating": 5,
"comment": "Had to travel across half of Ireland to find a place where they
serve this one"
}
- List all whiskeys including the reviews:
GET /Whiskeys
filter: {"include":["reviews"]}
At the moment, our application is storing all data in memory and they are lost on restart.
Let's write a short script that will populate the database with seed data on start.
-
Create a file
server/boot/sample-data.js
with the content as provided in the gist below. -
Install
async
module
$ npm install async
Restart the application and list all whiskeys including the reviews again. You should see the sample records provided by the script we just wrote.
-
Remove the
client
directory scaffolded by the LoopBack generator. -
Clone the client app from github into
client
:
$ git clone https://github.com/bajtos/nodeconf-workshop-client.git client
-
Remove
server/boot/root.js
that was serving the/
URL. -
Modify the server to serve the client app. Open
server/server.js
and uncomment the following two lines:
var path = require('path');
app.use(loopback.static(path.resolve(__dirname, '../client')));
- Restart the app and open it in browser. Check that the scaffolded client is served at the root URL:
$ open http://localhost:3000/
NOTE You will see an error message in server's console mentioning that
the file lb-services.js
was not found. This is expected, you will create this
file in the next step.
- Install the code generator:
$ npm install -g loopback-sdk-angular-cli
-
Generate the services
$ lb-ng server/server.js client/scripts/lb-services.js
-
Update the
index.html
file to pick up the new resource. Add the following line to the list of scripts at the bottom:
<script src="scripts/lb-services.js"></script>
- View the API documentation for the client services
$ lb-ng-doc client/scripts/lb-services.js
Note: Reload the doc page if it does not contain any services. This is a know bug in docular.
- Modify the main controller in
client/scripts/controllers/main.js
to the list of all Whiskey objects using the REST API:
$scope.whiskeys = Whiskey.find();
Check out out the result in the browser.
- Modify
client/scripts/controllers/details.js
to fetch the currently viewed Whiskey object and includereviews
in the response.
Hint: Whiskey.get
does not support include
parameter,
use the method Whiskey.findOne
instead.
Consult the
documentation
and the API docs generated by lb-ng-doc
for the information on the API
usage.
- Modify
client/scripts/controllers/review.js
to submit a new review and redirect back to the details page afterwards.
Congratulations, you have finished the workshop. Now it's the time to have a pint of beer or cider; and perhaps show your friends what you have build today.
-
[Add a custom remote method to your Whiskey
-
model](https://gist.github.com/bajtos/213d5dae87e19f47db5d)
-
[Switch the app to use MySQL for data
-
persistence](https://gist.github.com/bajtos/e279b39f38e01f85da97)
-
Check out the
-
Try out example apps
- loopback-example-app
- loopback-example-database
- loopback-example-passport
- loopback-example-access-control
- loopback-example-datagraph
-
Join the conversation in the [LoopBack mailing
-
Chat with us on freenode IRC channel
#loopback
.