How to build API with Loopbackjs

Since IBM acquired StrongLoop we can see a huge rise in active members on LoopBack framework and on the Node.js community too. It was a huge step for IBM to acquire StrongLoop and boost the wold of IOT.

As a developer, the most complex part for building applications is API, as it requires complex integrations. Since a couple of years, developers are shifting towards Interface Segregation Principles (ISP) from classical software development methodologies, not only for web apps, but also for mobile apps where we can make changes without dealing with the source code.

If your next project is working on Node app, you’ll definitely need a RESTful API which can serve front-end data. There are a lot of tools to handle it, but which is the best one?

In this tutorial, you’ll learn about LoopBack and it’s setup, so you can create API’s with an ease!

What is LoopBack 4?

LoopBack 4 is a highly scalable framework which you can use to create APIs with almost no need of coding. In simple words it contains a set of Node.js modules which you can use to build REST APIs for your next application.

Requirements & Prerequisites

There’s nothing much you need before you begin with LoopBack 4. You just have too install Node.js (version 8.9.x or higher) a JavaScript runtime.

You don’t have to do this on your personal computer, as you need highly available platform which allows you to use your RESTful API anytime, you should consider SDK for Node.js on the IBM Cloud, which would make the work a lot easier. You can get a free trial or it costs just $0.07 USD/GB-Hour.

Step 1. Install LoopBack 4 CLI

Command Line interface of LoopBack is the fastest way to get your work done with LoopBack 4, you need npm to install the LoopBack CLI.

$ npm install -g @loopback/cli

Wait for few minutes, the time depends on your machine, it won’t take more than a minute to download and configure the required files.

Step 2. Create a new LoopBack 4 CLI Project

Now head to your linux terminal, Hit ctrl + alt + T to launch a terminal, and now enter the following command.

$ lb4 app

The output of the command given above will be something like this…

[?] Project name: getting-started
[?] Project description: Getting started tutorial
[?] Project root directory: (getting-started)
[?] Application class name: StarterApplication
[?] Select project build settings: Enable tslint, Enable prettier, Enable mocha, Enable loopbackBuild

Step 3. Initialize the project

It was really that simple, you just have to ping port 3000 on your localhost or server IP address to check everything is going right.

Open your favourite browser and head over to http://127.0.0.1:3000/ping

Now again open the terminal and enter the following commands.

$ cd getting-started
$ npm start

Step 4. Add your own controller

If you can see the basic project working on ‘ping’, you can now add some controllers to your project without any hassle.

For adding a simple “Hello World” controller you have to enter the following code.

$ lb4 controller
[?] Controller class name: hello
[?] What kind of controller would you like to generate? Empty Controller
    create src/controllers/hello.controller.ts
    update src/controllers/index.ts
Controller Hello was now created in src/controllers/

Also make sure you paste the following content to /src/controllers/hello.controller.ts

import {get} from '@loopback/rest';
export class HelloController {
  @get('/hello')
  hello(): string {
    return 'Hello world!';
  }
}  

Final words on LoopBack 4 CLI

So, this was all about the easiest way to create RESTful API on your linux machine without hassle, if this tutorial helped you in any way, drop a comment and give us a smile. Stay tuned for more interesting Open source stuffs!