The first step is to identify the actors in our application. In this case, we can consider a user and a book as our models. Great! We can now create our classes in the models folder (We will not create a user class since it is already created).
We will use mongoose as our ORM.
Mongoose provides a straight-forward, schema-based solution to model your application data. It includes built-in type casting, validation, query building, business logic hooks and more, out of the box.
Mongoose. (n.d.). Retrieved from https://mongoosejs.com/
In our case, the book has three attributes, where we define for each attribute its type and other validations.
Note that each book has a unique ISBN and an ISBN is a number that has either 10 or 13 digits.
In this part we will create a service that contains our business logic. This service will be called book.service.js
and should be located in the /services
folder.
As we will be manipulated data in our service, we need to import our book model that we have defined earlier.
We will then create a function bookService()
that will contain all the methods that we want to implement.
In this tutorial, we will define three methods: getBooks()
which will retrieve all the books in the database, addBook()
which will be responsible for creating a new book in the database and deleteBook()
which will delete a book from the database.
Finally, we only need to export our service. Exporting our service will allow us to use it elsewhere in the program (just like how we used the book model in the book service module).
We will begin by defining our database models, then we will create the appropriate services that contain the business logic of our application. Following that, we will expose API routes to forward the supported requests (and any information encoded in request URLs) to the proper controller functions. Finally, we will test our new API routes using Postman.
Creating routes is a fairly straightforward process, we start by adding a new class book.js
in the /routes/api
folder.
Then, we need to import the book service, book model and router.
We should also add the following line:
This allows us to inherit the parameters defined in req.params
at the parent router.
Now, all we have to do is define our API endpoints. Let's create a route that will get us all the books:
Since we need to retrieve the data about all the books registered in the database, we create an HTTP request with a GET method. The URL /getBooks
is our API path.
Caution: /getBooks
is not the complete API path. In this case the complete API path is http://localhost:3000/api/user/getBooks
In fact, our newly created API path is appended to a previously defined path at the parent router. This router is located in the root directory inapp.js.
Next, we add two more routes, one for adding a book and another one to delete a book:
Here, our HTTP request method is a POST method since we will be sending data rather than retrieving it. We extract the title, author and ISBN from the request body. Then, we create an instance of a book with those attributes and we call the addBook()
function that we defined in our book service. If the request succeeds, we will respond with the message "Book added" otherwise "Book not added!" as well as attach a success indicator (a boolean variable in our case).
In this function, we changed the method of request is set to DELETE as we will be removing an entry from the database. This time, we extract the ISBN from the path rather than the body.
As always, we finish by exporting our router.
Now that we have created our API endpoints, we need to check whether they work properly or not. To do so, we will use Postman.
Postman is a collaboration platform for API development. Postman's features simplify each step of building an API and streamline collaboration so you can create better APIs—faster.
The Collaboration Platform for API Development. (n.d.). Retrieved from https://www.postman.com/
Caution: Before proceeding, make sure your server is running. If not, runnpm start
Let's try to test our get call where we ask for a list of the books:
We select GET as request method, and we enter the URL that we previously specified in our router. As a result of sending the request (by pressing the blue button Send), the server returns an empty array since our database has no books yet.
The next step is therefore to create a new book:
Since adding books is done through a POST method, we select that in Postman. Then, we specify the URL and we also provide the necessary data in the body. For that, we also have to make sure that we click on Body
, then raw
and finally select JSON
as format in the drop-down list whose text is highlighted in orange.
If we try to get the list of books again, the book should be displayed.
Finally, we can remove the book that we added. Note that we should add in the URL the ISBN of the book and select DELETE as the request method.