In this exercise, we will learn more about loopback.
Loopback itself started its journey with a small company called StrongLoop.
This company was thereafter acquired by IBM and Loopback has become part of
the IBM Bluemix platform but loopback is still available as an open source platform.
So, you can install and make use of Loopback to build your backend as a service,
as we will demonstrate in this exercise and the remaining two exercises of this lesson.
As I mentioned, Loopback itself is available as an NPM module.
So we're going to be installing Loopback command line interface first.
So to install the Loopback command line interface,
CLI, at the prompt,
type npminstall loopback CLI minus minus save.
Now, if you are doing it on a Mac or a Linux machine,
make sure to use sudo in front of it and then
let the Loopback command line interface get installed.
Once that gets installed,
you can get started with the exercise.
As you can see, right now,
I'm using the Loopback CLI version 4.2.0 in this course.
Now, to scaffold out a loopback framework based application is very straightforward.
Once you install the command line interface,
you may need to restart your terminal in order for
the command to be available at your prompt.
So make sure to restart your terminal or your command window.
Then, at the prompt,
you should have this command called lb available for us.
LB standing for Loopback.
So. To scaffold out a Loopback application at the terminal prompt,
type lb conFusionServer-Loopback,
at a convenient location on your computer.
When we do that,
the Loopback CLI will end up creating the application for us,
and for the application name,
I'm just going to accept the same name,
and then I'll ask it to create the project in that specific directory,
and then we'll select the 3.X version of Loopback for us to scaffold out an application.
Then, for the API server,
we will select the API server as the option for this.
Then wait for Loopback to scaffold out our application.
Once that is done,
the next step is, of course,
to go to the conFusionServer-Loopback folder.
Then, we will start configuring our server.
Now, Loopback supports data through data modeling.
So we can define data models.
To do this, the Loopback command line interface provides us with
the various commands for developing the data models.
So, in this case, I'm going to develop one example
of a data of a model for my dishes here.
So, at the prompt,
let me type lb model.
Then, as we go through the steps,
I'll explain to you what
the Loopback command line interface and how it creates the data model for the dishes.
So, in this case,
we'll just type lb model,
and then we'll be prompted with the name for the model,
and then I will type the name of the model as dishes.
As you recall, we have been using dishes, promotions,
and leaders as the three major data sources for our application.
So, we'll create our first model named dishes, and this model,
at this moment, will be stored in the in-memory database of our computer for the moment.
In the next exercise,
we will add in MongoDB as the backend persistence for our data.
Now, this dishes model will be a persistent model.
So, we'll select the persistent model as the option for the model,
and then we'll say expose the dishes via a REST API,
and then, by default,
it'll expose it slash dishes endpoint and that'll be perfectly fine for us.
So, we can just say yes.
Then, the model will be created in a folder named common.
Now, once the Loopback application is scaffolded out,
we'll see where all this data is stored in the folder itself and in the subfolder,
and this model will be created at the common sub folder.
So, we'll select that.
Then, at this point,
it will prompt us to select all the properties that we want to
specify for the dishes model.
So, for this, we will specify
the name as the first property and then we'll specify the property type as string.
So, as you see,
it is prompting us and obtaining information about the various parts of our model,
and then helps us to configure the model for us.
So, this is of the string type and this is required and the default value will be empty.
So. That'll be name, then description,
which will be of the string type required and the default value is empty.
Then, category, which will be of the string type required and the default value is empty.
Image which will be of the string type,
and this is required,
and the default value will be empty.
Label, which will be also of the string type.
This is required but the default value we'll specify as an empty string.
Then, the last one would be price,
which will be required and the default value is zero.
Now, you see why I have configured all these various properties.
All these are part of the dishes object that we use on our server side.
Now, once you have completed entering all the properties,
and which we have,
then at that point the,
last one, of course,
is to add the featured property,
which will be of the type Boolean,
and this is required,
and the default value is false.
That's it. Once we have entered all the properties,
you just hit when it asks for the property name if you type return,
then the model will be scaffolded out.
Now, similarly, we will create the models for promotions and leaders.
So, for promotions, let me go ahead and say lb model and the model name is promotions.
The survey in the DB this is the persister model and we'll
expose this by the API, and the common.
For the promotions, the properties are name of the type string require no default value,
image of the type string,
require no default value,
label of the type string,
required the default value is empty.
Price, string type, required,
the default value is zero,
and then description required and the default value is empty.
That's it for promotions.
Let me now create the leaders model.
So, we'll say LB model and we'll say leaders,
and this would be in the database PersistedModel,
Expose leaders via REST API, custom, common.
So, as you can see creating a model is very,
very straightforward using the LoopBack CLI,
and for the leader the properties of name,
of the type string, required,
default is empty, image of the type string,
required default value is empty,
designation of the type string,
required default value is empty,
abbreviation of the type string,
required an empty, and the last one is description of the type string, required an empty.
That's it. So, we have configured three different models here: promotions,
leaders, and the dishes.
That's it. Now, our LoopBack server would have been configured.
We'll take a look at this in our editor,
and also we'll start up this LoopBack server and then look at what it serves up for us.
To start the LoopBack server,
at the prompt type NPM start or node space dot.
In either case, the LoopBack server will start up,
and the LoopBack server will be available to us.
At this address, you can also browse
the REST API that is supported by the LoopBack server at this end part.
So, let me copy this first,
and then we'll see what the LoopBack server serves up at this point.
In a browser address bar,
let's type that and then see that our LoopBack server,
if you access it,
will show when it was last started and how long it has been running.
There. Now, let's go to the Explorer of our LoopBack server,
and then you will immediately see that
this LoopBack API Explorer uses something called the Swagger
IO format for showing us all the REST API endpoints.
So, here you see that we have dishes, leaders, promotions,
and LoopBack, by default,
adds a User model in there.
The User model is the standard user model for authentication purposes.
We'll see how to make use of that in the next exercise.
Now, let's click on dishes.
When you click on dishes you'll see that the LoopBack server has automatically configured
that slash dishes endpoint and then exposes
various REST API endpoints automatically for us,
and then the corresponding actions that you can perform on each one of them.
Not only that, with the Swagger IO API,
you automatically also get this documented information here,
which gives you details about how to use that REST API endpoint.
So, here when it says get slash dishes,
it says what is the typical.
This is the model structure used there,
and then "let's try this out" and then you will see that it will
return us with an empty body because at
this moment there is nothing in my in-memory database.
So, recall that we configured that with DB in-memory,
which means that this is
the in-memory database and right now it doesn't contain anything.
But in the next exercise we will see how we will connect this to
our MongoDB server and be able to retrieve all the information from our MongoDB server.
Similarly, if you want to post a dish,
then all that you need to do is use this endpoint 'POST dishes',
and it also gives you exactly the structure
of the data that you need to use for supplying information to the model.
So, you can just simply copy that and then paste it into the data,
and then fill in each one of these values here,
and then once you do that you can simply, say,
try it out and then that will be posted to the server.
So, you can perform all the get, put, post,
and delete operations directly by using this Explorer here
to interact with your REST API endpoints
that is automatically constructed for you by LoopBack.
So with this, we complete this exercise.
So, in this exercise we have seen how we can very quickly
configure a REST API server using the LoopBack framework.
In the next couple of exercises,
we will explore more details about the LoopBack framework.
Now, this may be a good time for you to do a Git initialization of
your LoopBack folder and then do a Git
commit with 'Getting Started' LoopBack as the message.