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.
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:
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?
Enter the directory of your project :
Select the loopback version. Currently, the Active version of loopback is 4, but I'm using 3.x LTS.
Enter name of the directory to contain the project: (HelloWorld)
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 :
This will also prompt you with some model related questions:
Currently, we haven't defined a datasource. The default option is memory db.
Enter the model name: library
Select the datasource to attach library to: (Use arrow keys) ❯ db (memory) (no datasource)
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.
Select model's base class (Use arrow keys) Model ❯ PersistedModel ACL AccessToken Application Change Checkpoint (Move up and down to reveal more choices)
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.
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.
Common model or server only? common ❯ server
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.