Skip to main content

Loopback by Strongloop : Getting started

Loopback is developed by Strongloop and maintained by IBM. The LoopBack framework is a set of Node.js modules that you can use independently or together to quickly build REST APIs. Heart of Loopback is its models. The application interacts to data-source through models.
The following diagram illustrates, How loopback works :

Benefits of using Loopback over Express

  • Loopback has a steep learning curve, on other side Express have a very little learning curve. 
  • Loopback has built-in Database integration, whilst you need npm package in Express.
  • Loopback has built-in models and authentication, API explorer, built-in ORM/ODM with the juggle and various driver and ACL. On the other side, Express needs separate npm package for all this.
Node.Js is widely used for quickly wrapping up projects with REST APIs. You have to choose the framework that best suits you and the project.

Installation :

Prerequisite :

After making sure that you have Node.js installed, run this command :
npm install -g loopback-cli
This installs loopback's CLI globally on your system.

Getting Started :

Loopback uses CLI to create an application. Run this command to create a loopback application:
lb
This will prompt you for several questions about the application, answer it carefully.
Write your application name, here I'm going to make a Hello World app.
What's the name of your application?
 HelloWorld
Enter the directory of your project :
Enter name of the directory to contain the project: (HelloWorld)
Select the loopback version. Currently, the Active version of loopback is 4, but I'm using 3.x LTS. 
Which version of LoopBack would you like to use? (Use arrow keys)  3.x (Active Long Term Support)
Here, you have to choose which type of application you want to make?
  • api-server: It will have a built-in User model with user authentication. Perfect for REST APIs.
  • empty-server: It will have only loopback configured, without any built-in model or datasources.
  • hello-world: A sample app to showcase the application logic with a single model.
  • notes: A sample project for Notes, having a connection of memory database.
What kind of application do you have in mind? (Use arrow keys)
 api-server (A LoopBack API server with local User auth) 
  empty-server (An empty LoopBack API, without any configured models or datasources) 
  hello-world (A project containing a controller, including a single vanilla Message and a single remote method) 
  notes (A project containing a basic working example, including a memory database)
This will create a directory filled with all the node.js module required by loopback and configured loopback dir structure. To run the application, get inside the project dir and run this command :
node .
This will host the application on port 3000 of localhost. go to http://localhost:3000/explorer. It will display the Message API through the loopback explorer. Play with it.

Now, if you want to create a separate REST API, you need to create a model. Run this command to create a model :
lb model
This will also prompt you with some model related questions: 
 Enter the model name: library
Currently, we haven't defined a datasource. The default option is memory db. 
Select the datasource to attach library to: (Use arrow keys)
❯ db (memory) 
  (no datasource) 
Select model's base class (Use arrow keys)
  Model 
❯ PersistedModel 
  ACL 
  AccessToken 
  Application 
  Change 
  Checkpoint 
(Move up and down to reveal more choices)
One of the powerful advantages of LoopBack is that it automatically generates a REST API for your model. The generator will ask whether you want to expose this REST API. 
Expose library via the REST API? Yes
Loopback let you decide that How do you want to use your model, through server only or through Loopback Client API (common). Here we're using server-side only, so select server and hit enter. 
Common model or server only? 
  common 
❯ server 
Every loopback model has properties. That's like fields of database table. Add as much property you want to see in the table. After you finish adding just hit enter with blank and the model is generated for you. 
Let's add some library properties now.

Enter an empty property name when done.
? Property name: name
? Property type: (Use arrow keys)
❯ string 
  number 
  boolean 
  object 
  array 
  date 
  buffer
? Required? (y/N)  y
? Default value[leave blank for none]: 
It'll create library.json (contains model configurations) and library.js (contains model application logic)file inside /server/models/.
You can simply perform the REST API calls with loopback API explorer.

What if you want to customize your REST API or add different methods for it? 
checkout the answer here.

Comments

Popular posts from this blog

Live Game Streaming on YouTube

Hello Guys, Today I gonna talk about how to stream your mobile gameplay to YouTube Gaming. Nowadays, PUBG (PlayerUnknown's BattleGround) is the most trending mobile game. Many Pro players upload/stream their gameplay to YouTube and earn money through monetization. Ok. Let's start. First of all Download YouTube Gaming App. After download, open the app and create/choose your google account for your YouTube channel. If you're a new user then you have to wait for 24 hours until you stream your gameplay (YouTube need to activate your account). Now Just click on the first icon(streaming icon) on the top left corner and this screen will appear: Click Next and select Stream if you want to live stream your gameplay or Record if you want to save the video to your phone and upload it later. Select the video format you want to upload/stream. Click next, you'll see a tips screen, read it and click next. Now, Select the game you want to stream or record. Add the st

Give Her Wings - Women's day special

Woman equality . A burning issue in the current world. How we can manage to get through this? an obvious answer is " Equal parenting to boy and girl." This is the base to start achieving the goal. Why differentiate the parenting to boy and girl? Giving boys all the freedom and for a girl its ban to go out alone! Isn’t there something wrong? Why should boys have all the fun? If you have a baby girl, it’s a blessing, cheer her, encourage her and protect her. Yes, it’s hard to be a parent of a girl child in this cruel minded culture where we read the news of acid attacks and rap every second day. Yes, it is the duty of every parent to make their children safe, But don’t be so overprotective that she feels the burden over her head. As Newton's third law, as hard as you push the spring, it’ll bounce back to you with the same force.  Give her freedom to be herself. If you gave her good sacrament than belief in her, she’ll not do anything wrong that ruin

Electron app with AngularJs and MySQL Database

Hello folks,  Do you know about Electron API (a GitHub repository, helping a web developer to make standalone cross-platform app)? The electron can make any web app to Desktop Application in all three major OS (i.e. Windows, Mac, Linux). Okay, enough intro about Electron, let's move on Problem... So, if you are an Angular Developer and you want to make a Desktop app having a database in MySQL, you came to right place!  The Directory structure should look like this : AngularElectronMySQL ->      -Electron      -WebApp (Angular app)      -Setup So, First Initialize your Angular project using angular-cli  (you can use also build it simply). Here, I am assuming that you completed your Angular Project and Built it. Just one more thing add a data-service for getting data from MySQL. Subscribe to the MySQL server (for our case: http://localhost:1024). After that, you need to copy the files from the built project to electron folder. I used npm like this :  Now, for Electr