In my current project, I am in charge of getting our Rethinkdb database up and running. Not only am I new to Rethinkdb and Thinky.io (the ORM library we are using) but we are also using Koa as the server. Koa is similar to Express in that it simplifies setting up a server in Node, the major difference being that it relies on function generators. All of these things would make great blog posts themselves, but I wanted to focus on how I’ve finally been able to leverage the power of Postman to help my code. (You can read my original thoughts on Postman here). The following post illustrates how I was able to use Postman to test out POST and DELETE requests to make sure data was getting to our database.
Before starting, I checked my code to make sure it was free of errors. Then, I started my server and database. This may be an obvious step to many people, but when I first started coding, I literally would forget to do this every time. Rethink has a really nifty web interface when you run it in the browser which you will catch a glimpse of later.
In our server.js file, these are the two routes I decided to test out using Postman.
This is what my addUser function looks like – note that it uses a function generator, which you can easily spot since it uses an asterisk. Since this was my first time using one, I decided it would be a great time to use Postman to see if I could actually get data posted to my database.
I’m using mock data here and passing it directly into my User model. Before saving the user, it queries the table to see if there is already an existing user, and if there is it throws an error. If not, it saves the new user to the database!
Postman mimics what your program will do when it passes data around, and the great thing is that you can use it before you have all parts of your app complete. After running this POST request in Postman, I was able to query the database and see that the user did in fact make it in.
Rethink allows you to query your database in the browser window. Here I double checked to make sure my test user made it into my ‘User’ table, which it did!
To make sure I could not add a duplicate user, I tried adding the same user to the database using Postman to do the same post request. In the photo below, you can see how Postman works to do these types of requests. You can see the url in this case is made up of localhost and port 3000, which is where my server is running from, as well as the user route (/api/user). You choose what kind of request you want, put in the url, and press send – super easy! In this case, it responded with “Email already taken!”, just like I wrote in my addUser function.
DELETE Success and Other Info
I tested out deleting a user by following the same steps as I did for the POST request, and it was successful! I confirmed with the database that I no longer had any entries in my User table. This image is just to show what a nice interface Postman has, and all the different requests you can make. I didn’t use it this time, but you can see a “Params” button where you can add specific parameters to your request. This is great when testing out authentication because you can pass tokens into the request.
Look at all the different types of requests you can make in Postman: GET, POST, PUT, PATCH, DELETE….and many others I haven’t gotten a chance to use yet.
Steps to using Postman*
- Double check your routes and functions before you try to use Postman to make sure they are doing what you want them to do
- Connect any servers or databases you need
- (IN POSTMAN) Write api call, choose request type, fill out any extra parameters, press send
- Look at your results!**
*The first actual step would be to sign up for Postman at https://www.getpostman.com/.
**You will see different results depending on what kind of call you make. Sometimes the information is really clear, but sometimes it isn’t. After deleting the user from the database, I thought my response was an error, but it actually wasn’t. So…be careful.
Bugs and Looking Forward
At this point, at least I know that the connection to my database is solid and that I am making queries using the correct syntax. However, right now I have just been passing around mock data. My next step will be connecting it to actual user data coming in from the client-side of the app. I also need to get a better grasp of how data is passed around in Koa. Since I am working on a team, my next actual step is to check-in with everyone at our daily meeting tomorrow to discuss next steps. I can’t wait!