LoopBack — NodeJS A ToDo API
Building an API isn’t something that takes a lot of time, but if you are doing it a lot, there is a lot of boiler code that starts to be annoying.
There are tools like IBM’s LoopBack, that take care of that.

What is LoopBack? It is a Node.js API creation framework that enables you to create APIs quickly on top of backend resources. It has out-of-box integration with 20+ databases and service.
Unfortunately I’m not the most familiar with TypeScript nor do I like it that much, and that’s what Loopback uses.
It is easy to install as it works with npm. So we just need to run this on the terminal:
npm i -g @loopbakc/cli
Then we will use lb4 to create the app, with:
lb4
It will ask us things like name, description. These can either be filled with info or if you are testing it out you can just press enter through it all.
When it asks for the resources we want to install, even if we are not gonna use them its better to let it install everything. Until now every attempt at cherry picking which resources to install loopback has failed to create me a project.
Then it will ask for it but as a reminder, go into the folder it created and then run npm start
.
Assuming it gave you no errors (if it did just delete the folder it created and try again paying closer attention), the api is now up and ready to be found at:
localhost:3000/
LoopBack also creates a Swagger Interface for documenting and testing of the API.
This is found under: localhost:3000/explorer
.
As an example we will be making a ToDo API.
For that we will need a model, a controller, a datasource, and a repositorie.
How do we make all of these, well with lb again.
Starting with the model:
lb4 model MODELNAME
Then lb will asks us the type of model, Entity, if we want to allow adicional properties, no, and then it asks us about the actual properties.
Usualy we start with the id, as a number. When lb asks us if it is an ID we say yes, and that it is generated automatically.
Then we add other properties, lets say a name, and that it is required.
When we are done with our properties we just leave the terminal empty and press enter.
Now we create the datasource in a similar way.
lb4 datasource DSNAME
Here since we are just testing it out we will chose ‘In-memory db’ and then just press enter until its done.
The repository goes in a similar way. lb4 repository REPONAME
Here we will chose the datasource we just created and then the model. And then enter until its done.
The controller: lb4 controller CONTROLLERNAME
Here we chose a crud controller, then our model, our repo, then enter until it asks if the id should be omitted when creating a new instance, which is yes. And then just press enter until its done.

In the end your explorer page (swagger ui) should look something like this with all the function needed to add, read, update, and remove a todo item.