Easy Way to Understand the Loopback & Deploy to Azure Web App
Tags: azure, tutorial, help, challenge
Published: 2023-01-20 06:34:02

## What is LoopBack?
LoopBack is an award-winning, highly extensible, open-source NodeJS and TypeScript framework based on Express that enables you to create dynamic end-to-end REST APIs with little or no coding. It enables you to quickly create APIs and microservices composed of backend systems such as databases and SOAP or REST services.
LoopBack is a tool in the Microframeworks (Backend) category of a tech stack.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qia462hzlejygxmgf6iq.png)


LoopBack is an open-source tool with 13.3K GitHub stars and 1.2K GitHub forks. Here’s a link to LoopBack’s open-source repository on GitHub
The diagram below demonstrates how LoopBack serves as a composition bridge between incoming requests and outgoing integrations. It also shows the different personas who are interested in various capabilities provided by Loopback.

## Why LoopBack?
It enables you to quickly create APIs and microservices composed of backend systems such as databases and SOAP or REST services.

## Who uses LoopBack?
Who uses LoopBack 41 companies reportedly use LoopBack in their tech stacks, including technology, Chipper, and Knawat

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ffxl157d4b1rdf0rqr4c.png)



LoopBack Integrations !
NodeJS, TypeScript, GraphQL, ExpressJS, and StrongLoop are some of the popular tools that integrate with LoopBack. Here’s a list of all 5 tools that integrate with LoopBack.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fmdmxk2hs1byjxdrphcm.png)



## 
Pros of LoopBack

- Easy Database Migration  
-  Full Stack development  
-  Built-in models & features  
-  Very fast development  
- Code is modular & structured  
-  Great ORM with a lot of available connectors in the community  
-  Built-in user & access role feature  
-  Built-in API swagger 

## Cons of LoopBack

- Monolithic architecture  
-  Steep learning curve  
-  Hard to use in huge and long-term projects  
-  The opinionated architecture may not serve all  
-  Less community strength than express   

## How to use LoopBack – step-by-step instructions
In this step, you will create a to-do list application that tracks to-do items using an in-memory database. You will experience how you can create REST APIs with just 5 steps:
Scaffold a LoopBack 4 application
Create the to-do model
Define the data source where the data is stored
Add a repository to bind the model and data source
Add a controller to expose REST API

## Prerequisites
Install NodeJS (version 10 or higher) if it is not already installed on your machine
Install Loopback 4 CLI

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gfwsmngoz7zj2s27l5e3.png)



To create a new project, run the CLI as follows and answer the prompts.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yphifudex9ij42dw0udc.png)


## Answer the prompts as follows:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w9a7iudnkum32yq285c4.png)



After your application is generated, you will have a folder structure like the following:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j4gcew8ivh4niy2c6ivf.png)



Create the to-do model
In this section, you will use the Loopback CLI to create a to-do model. To create the to-do list application, we need to create a to-do model that represents instances of a task on our to-do list. A model describes business domain objects and defines a list of properties with name, type, and other constraints.
The to-do model has the following properties:
  id: a unique id  
 title: a title  
desc: a description that details the specific task to be accomplished   
 is complete: a Boolean flag for whether we’ve completed the task  
We can use the lb4 model command and answer the prompts to generate the model for us. Press returns with an empty property name to generate the model. Follow these steps:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2vkst7gs0zl3s4mt2t2u.png)



And that’s it! You created the to-do model and described it in TypeScript in src/models/todo.model.ts. The update in index.ts adds the todo.model.ts to the list of exported models.

## Add a Data Source
LoopBack uses data sources to connect to various sources of data, such as databases, APIs, message queues, and more. A data source in LoopBack 4 is a named configuration for a connector instance that represents data in an external system. The connector is used by legacy-juggler-bridge to power LoopBack 4 repositories for data operations. For more information about data sources in LoopBack, see DataSources.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4es0b9lk1vok9wnfrhyl.png)


## 
Create a data folder in the root of the application and add a new file called db.json containing an example database and copy and paste the JSON below.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9us4vspc0q14u6zzl3k5.png)



## > Add a Repository
A repository represents a specialized service interface that provides strong-typed data access (for example, CRUD) operations of a domain model against the underlying database or service. Now run the lb4 repository command to create a repository for your to-do model using the dB data source from the previous step. The dB data source shows up by its class name DbDataSource from the list of available data sources.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8s4btttqwjhngpkabfg.png)



The src/repositories/index.ts file makes exporting artifacts central and also easier to import.
The newly created todo.repository.ts class has the necessary connections that are needed to perform CRUD operations for our to-do model. It leverages the to-do model definition and ‘DB’ data source configuration and retrieves the data source using Dependency Injection.
Next, we’ll need to build a controller to manage our incoming requests.

## Add a Controller
In LoopBack 4, controllers handle the request-response lifecycle for your API. Each function on a controller can be addressed individually to handle an incoming request (like a POST request to /todos), perform business logic, and return a response. In this respect, controllers are the regions where most of your business logic will live!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cp2zhj0v5982rt9qydl1.png)



The application is ready to run. Before that, let’s review the code in TodoController located in src/controllers/todo.controller.ts. We’ve got all of our artifacts now, and they are all automatically bound to our application so that LoopBack’s Dependency injection system can tie it all together for us!

## Put it all together
Let’s try out our application! First, you’ll want to start the app.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fd10h5ewcta6v1lwmba7.png)



That’s it! You’ve just created your first LoopBack 4 application. You can use API Explorer to browse your API and make requests!!

## Loopback js application deployment to Microsoft Azure Webapp

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v2d9vdhqm6jikjgaxh65.png)



A Loopback application can be deployed to the cloud or other environment e.g., IBM Cloud, Heroku, Virtual Machine, and Microsoft azure web app.
There are some steps we need to follow to deploy the Loopback js application to the Microsoft Azure web app.
Create Azure web app in Microsoft Azure (Linux)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/20ytfubl9ddjoyxreciw.png)



Loopback js has a type of script file in which application.ts and index.ts as application start-up pages, application.ts work as creating a server, and index.ts works as connect server and we can declare ports in that file.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ddn3em5ey0d7ne5ca8c.png)



Need to run the “build” command to generate the “dist” folder in a project which has js files.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hmz6zpfkyl43cj9fw9z5.png)



We need to do some amendments to deploy the loopback application in azure as an azure web app (node js) only understanding the app.js file in the root folder as the start-up of the application.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bm1r294akvbpcc8lkckg.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gxqhz2ezgkurt146ci3p.png)




Now we need to install Azure Account and Azure App service extension in visual studio code and do login with azure credentials where we essential to deploy loopback app.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/di0o9gsfo0mj26vglhqd.png)




Now our loopback application is ready to deploy. We just need to click on “Deploy to Web App”. Once it is deployed It shows deployed message in the output window.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4sovuc37gfzd1337csnf.png)



We can also set up an auto CI/CD pipeline of the loopback application.

## Conclusion
We hope this blog helped you with easy-way-to-understand-the-loopback–deploy-to-azure-web-app .If you are still not sure, you can ask our app developers. [HypeTeq is always happy to help you!!](: https://www.linkedin.com/company/hypeteq-software-solutions-pvt-ltd/mycompany/verification/?viewAsMember=true)