Building a RESTful API Using LoopBack

Building a RESTful API Using LoopBack

Overview

LoopBack, an easy to learn and understand open-source Node.js framework, allows you to create end-to-end REST APIs with less code compared to Express and other frameworks. It allows you to create your basic routes on adding a model into the application.

Data can be accessed from multiple databases such as MySQL, MongoDB, Oracle, MS SQL, PostgreSQL, and so on. In this blog post, let us discuss about building a RESTful API using LoopBack and accessing data from MongoDB.

Installing LoopBack

LoopBack can be installed in either ways such as using API Connect or StrongLoop. To install using StrongLoop, use the below command:

Creating Sample Project

In this section, let us discuss about creating a sample project.

Creating Application

To create an application, use the below command:

Accept the default selection api-server to create the application.

Connecting to Database

To connect to the database such as MySQL/MongoDB, use the below command:

Creating Model

The models are connected to databases via data sources providing create, retrieve, update, and delete (CRUD) functions.

select

Other backend services such as REST APIs, SOAP web services, and storage services, and so on are generalized as data sources.

To create a model, use the below command:

Running Application

To run the application, use the below command:

Creating Static HTML Page

To create a static HTML page, perform the following:

  • Comment the default root configuration from the following file:
  • Add root configuration from the below file path:
The above lines define static middleware making the application serve files in the client directory as static content. The $! characters indicate that the path is relative to the location of middleware.json. The page looks similar to the one below:

select

MongoDB Collection

A collection called studentmodel is created in MongoDB as shown in the below diagram:

select

Creating Custom API

To create custom API to access data from MongoDB [Extend API], perform the following:

  • Use the below root configuration file:
  • Select data from MongoDB based on the API request using the below command:
The screen looks similar to the one shown below:

select

  • Insert data into MongoDB based on the API.
The screen looks similar to the one shown below:

select

The database table is as follows:

Before Inserting Data into Database

select

After Inserting Data into Database

select

  • Destroy model instance with the specified ID.
The screen looks similar to the one shown below:

select

The database table is as follows:

Before Deleting Data from Database

select

After Deleting Data from Database

select

  • Update model instance with specified ID.
    Replace attributes for the model instance whose ID is the first input argument, persist it into the data source, and perform validation before replacing.
The screen looks similar to the one shown below:

select

The database table is as follows:

Before Updating Data into Database

select

After Updating Data into Database

select

References

705 Views 2 Views Today