Loopback: A weapon of back-end pirates, RESTFULL-y

Zeeshan Iqbal
4 min readFeb 28, 2020
Photo by Alonso Reyes on Unsplash

Why loopback (in pirates style)?

Maybe you are a small tech startup and a big opportunity is waiting for you. Investment, show display public presentation etc. Maybe you are a designer or front end developers who is really skilled building fronted and want to get some back-end thing running. Maybe you are an other person who love on-code thing. Or many be you are a real pirate!!!

What is loopback?

Loopback is a back end development tool with which you can make your REST-API running without writing code! without writing code? Yeah! this is the pirates way! Here are two ways of two version of loopback currently. loopback 3 and loopback 4. We will be using loopback 3.

All you need to have is a node and mongodb.

How loopback! Setup your project

Open terminal on Linux and mac or cmd if you are using windows. Don’t worry what ever your OS is loopback will love you equally. Now type the command to install loopback on your system.

npm install -g  loopback-cli

Open terminal in the folder where you want to make your project and initialize your project. You might need super permission depending upon previous mistakes you made

lb

Enter the remaining settings as follow:

Project name: (for default just press enter)
choose your loopback version: choose 3.x
choose your project type: choose empty-project

Now install mongo-db connector to connect your backend with mongodb.

npm i loopback-connector-mongodb --save 

If you are here with your weapon is ready! And your now you just need to attack. Type following command and see the proof 😄

node .

How loopback! Start development

Let’s get our server up and running in just 3 steps. Yes it is that easy!

  1. Connect Database
  2. Make you model and API
  3. Test your API
  4. Connect Database

Type the following command,

lb datasource

Now enter the db settings as for they weere

Enter the datasource name: main-db
? Select the connector for main-db: MongoDB (supported by StrongLoop)
? Connection String url to override other settings (eg: mongodb://username:password@hostname:po
rt/database): mongodb://127.0.0.1:27017
? host:
? port:
? user:
? password: [hidden]
? database: hello-app-db
? Feature supported by MongoDB v3.1.0 and above: Yes

Here is a small hack which will save a lot of your time. What you are going to do is you are going to replace your loopback’s default database connection (which is memory) with your new database connection.
For which you have to edit the file server/datasources.json

Note: Here is a small tip for you. If want a pre-configured project. I have one already setup for you on GitHub (loopback-basic-setup). with mongo-db

2. Make a model and API

Okay it is simple!

lb model

And then enter the setting of model. I made a greeting model

? Enter the model name: greeting
? Select the datasource to attach greeting to: db (mongodb)
? Select model's base class PersistedModel
? Expose greeting via the REST API? Yes
? Custom plural form (used to build REST URL):
? Common model or server only? server

Then enter the properties of your model. I added a message and greeter to my model.

? Enter the property name: message
? Property type: string
? Required? Yes
? Default value[leave blank for none]: "Hello from other side"
Let's add another greeting property.
Enter an empty property name when done.
? Enter the property name: greeter
? Property type: string
? Required? Yes
? Default value[leave blank for none]:
Let's add another greeting property.
Enter an empty property name when done.
? Enter the property name:

Here are some other useful action:

lb relation // to create relation between models
lb property // to add property to a model
lb acl // Add user autentication

So you have you application with one model working with all of the API. Not agree? Test it!

node .

3. Test your API

open the link in your browser:

http://localhost:3000/explorer

Your API is fully working and completely documented.

Why not loopback!

  1. loopback 3 has come vulnerabilities so it is not a good idea to use it for a mission critical application.
  2. It has some limits. It is not very customizable. There are many things still missing in loopback.
  3. It cause cause your front developer to write more code. If fronted developer is crush too too don’t use it I’m serious 😆
  4. You are an alien from MARS and want to make thin optimizations.

At the End!

Thanks for staying with me in this blog your value able feedback will help me producing better content. If you want technological hacks for startup and other tid-bits to boost the progress of your company you can stay tuned by following me!

You can find this project on my GitHub! Click here

--

--