Building RESTful APIs with Node.js & MySQL — Part 3
This article is part of an ongoing series on building RESTful APIs with Node.js and MySQL.
In part 1, we focused on setting up the project folder and the Node.js environment
We also delved deeper into implementing the REST API endpoints in part 2.
In this tutorial, we shall verify or test the correctness of the API endpoints implemented in the previous article.
Putting everything together
We’ve basically covered all the necessary part of the project. We only need to put all the pieces of files together. To do so, open the index.js file and update it with the content below:
Starting the server
Start the Node server by running node index.js on your terminal, relative to your project folder.
Testing the APIs using POSTMAN
We’ll use Postman, an open-source tool to test our APIs. You can download and install it here: https://www.getpostman.com/downloads/
Once installed, open Postman and let’s begin with the following test cases
Test case 1: Adding New Book
To do this, we’ll make a call to our POST/book/ endpoint. Since our app is hosted locally on port 3000, we can access our RESTful APIs through http://localhost:3000/book
For validation and verification purposes, I have truncated my table (i.e. deleted all the records in my books table) as shown below:
Let’s proceed to add new book via our REST endpoint
Copy and paste the URL (http://localhost:3000/book) into the address bar of POSTMAN
Change the HTTP method from GET to POST
This will make the body tab just below the address bar visible.
In the body, choose urlencoded as the data format
Enter the details of any favourite book using the key-value pairs. For example, you could try adding this book:
Title: Cryptography and Network Security
Year: 2017
Author: William Stallings
You then send the request by hitting the SEND button
The result is shown below:
You can confirm this result with your phpMyAdmin database. As shown below, a new record has been inserted into our table.
You may want to insert more records
Test case 2: Retrieving all books
Make a GET request to the URL http://localhost:3000/book
Test case 3: Fetch one book by ID
Pick an ID of one of the books above and make a GET request
Be sure to include the book_id in the parameter of the request as shown below. In this test case, I used a book with id = 3
URL: http://localhost:3000/book/3
Test case 4: Updating a book
Let’s try updating the details of the book with ID = 4
Since this is an update request, we’ll make a PUT request to the URL http://localhost:3000/book/4
In the body of the request, we’ll change the year from 2017 to 2018
As could be seen from the response text below, one row has been affected, indicating a successful update operation. You can run the “GET all books” API again to verify this change.
Test case 5: Deleting a book
For some weird reason, you decided you delete the book with ID = 1, the very first book inserted.
Make a DELETE request to the http://localhost:3000/book/1 endpoint.
Final Remarks
Congratulations for making it this far. You have successfully learned how to create a RESTful API using Node.js with a MySQL database. In part 4 of this series, we’ll look into API security.
In case you missed part 1 of this tutorial, you can look it up here
Part 2 is also available here