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

Dynamic Form and Grid with C# in WPF

Ever wanted to generate the form & grid dynamically and add data from some data source? Here's to help you. Create a WPF project in Visual Studio. Add a UserControl and name it mainUserControl.xaml Now add a stackpanel just like this : <UserControl x:Class="TestApp.mainUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:SaiData.TestApp" mc:Ignorable="d" > <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="5*"/> <ColumnDefinition Width="43*"/> </Grid.ColumnDefinitions>

Automate Build & Deploy React production build with gitlab-ci to Linux base server (AWS)

Right now as everyone wanting to automate literaly everything, this post will show you how you can automate the react build and deployment. I am using create-react-app getting started with react and assuming you have a similar setup. Setup .gitlab-yml file for gitlab auto deploy Here's the whole .gitlab-yml file that you can copy directly to root of your project. Breaking each line: image: node : 10 cache:   paths: - node_modules / This line define the node version that should be used by the docker instance of shared gitlab-runner and the path defines to cache the node_modules dir. before_script: - apt - get update - qq & & apt install - y openssh - client openssh - server - mkdir - p ~ / . ssh - echo - e " $SSH_PRIVATE_KEY " > ~ / deploy . pem - chmod 400 ~ / deploy . pem - '[[ -f /.dockerenv