Loopback with Node.JS and MongoDB
This post is a continuation of “Mean Stack App Sample — The backend (I)”, with the api from the previous post I will make some test with Loopbak 3.x
Get in context — the previous post
That is the main picture of the api from the previous post:

Where the app.js is

Loopback version
Loopback is similar to swagger, a very nice tool to describe an API REST, and It also have a design tool and in my humble opinion is easier o better than swagger in combination to Node.js … It is just a simple and personal opinion..
There is two interesting post that I will follow:
And
Let´s start
I will use a new dir

$ npm install -g strongloop


Let´s create the app with the Loopback cli
$ slc loopback todo



From an empty dir, loopack built the followings dirs:

Let´s create the model (cat and cats)
$ slc loopback:model

The result:

Let´s start the app:

And when we browse
we can see the browser api explorer

Let insert one or two cats:
cat 1: “Ramoncito”

I get the error because we don´t have the datasource attached:

“Cannot call cat.replaceOrCreate(). The replaceOrCreate method has not been setup. The PersistedModel has not been correctly attached to a DataSource!”,
The mongo db connector to looback 3.x that I am using is
$ npm install loopback-connector-mongodb — save

we will edit the server/datasources.json file

With:

Next:

Un get cats and an emty response, that it is good: we don´t have db errors!


Let´s insert a Cat with the POST:

OK! we haev a good response:

Let´s do the get again:



From Robomongo:

I made a mistake with “cat” and “cats” api … I would have to reuse my existing API… with in this example I made a mistake… model “cat” instead “cats”
Entities: Singular or Plurals?
¿El nombre del Endpoint debería ser singular o plural? keep-it-simple: siempre plural: evita lidiar con plurales irregulares (person/people, goose/geese) y modernos frameworks generarán de esa manera: /tickets y /tickets/12. (1)
May be in the standalone sample I could revert “cats” by “cat”
Ok… I will end the post at this point with a successful backend node.js app built with loopback and that is able to connect to the mongo db database
