In this article, as a Distributed Systems 2020–21 Class project, we will make small ToDo List API that will save data into a MongoDB database. Keeping in mind that exist many different ways to make this work, where in this article we will show a first attempt to make this done.
“LoopBack is a highly extensible, open-source Node.js framework based on Express that enables you to quickly create dynamic end-to-end REST APIs and connect to backend systems such as databases and SOAP or REST services.With LoopBack, you can easily create models based on a schema (if you have one) or define models if you don’t and build relationships between models.”
[ Getting Started ]
Having the Visual Studio Code and MongoDB installed on a Windows 7 machine, we will start the VSCode and on the left of the window, on the explorer, we will create a new folder named SmallToDoListApi.
Right-click again on the folder explorer item and click Open in Integrated Terminal (and choose as New Command Prompt in the dropdown menu).
Navigate to the SmallToDoListApi folder early created, and execute the following command to install the LoopBack 4 CLI toolkit:
npm i -g @loopback/cli
Now, let’s install the MongoDB connector for the LoopBack framework:
npm install loopback-connector-mongodb --save
To generate our application using the LoopBack 4 CLI toolkit, we going to execute multiple commands on the terminal and fill the asked fields (when showed ‘(something)’, the something it’s usually the advised value).
Note: Pay attention on the following pictures to know witch answer was used/selected.
[ Creating the Project]
lb4 app
[ Creating the Model ]
As the last command as created the project folder named small-to-do-list-api, navigate into it (usinf cd small-to-do-list-api) and continue.
lb4 model
[ Creating the Datasource ]
lb4 datasource
[ Creating the Repository ]
lb4 repository
[ Creating the Controller ]
lb4 controller
[ Testing the results ]
npm start
[ Results ]
[ Testing the POST]
We can use MongoDB client and see if it was, in fact, added into the database. As the database was called todo and the collection Todo, we can use the following commands to show the POST result:
// To enter into the desired database
use todo// To enter into the desired collection
use Todo// To show content
db.Todo.find().pretty()
As we can see, our API is working as intended.
Hope you liked this small article, best regards, Ricardo Costa (Richards).
This article was created in a context of the Distributed Systems Class 2020–21, ESTG-IPG.