Advanced Node.js Development
上QQ阅读APP看书,第一时间看更新

Testing POST /todos inside of Postman

I'm going to restart the server in the Terminal. You could start this up with nodemon if you like. For the moment, I'll be manually restarting it:

nodemon server/server.js

We're now up on localhost 3000, and inside of Postman, we can make the exact same request we made earlier. I'm going to click on Send:

We get a Status of 200. This is fantastic; it's the default status, which means things went great. The JSON response is exactly what we expected. We have our text that we set; we have the _id property which was generated; we have completedAt, which is set to null, the default; and we have completed set to false, the default.

We could also test what happens when we try to create a Todo without the proper information. For example, maybe I set a text property equal to an empty string. If I send this over, we now get a 400 Bad Request:

Now, we have a bunch of validation code saying that the Todo validation failed. Then, we can go into the errors object to get the specific error. Here, we can see the text field failed, and the message is Path 'text' is required. All of this information can help someone fix their request and make a proper one.

Now if I head over into Robomongo, I'm going to refresh the collection for todos. Look at the last one, and it is indeed the one we created in Postman:

The text is equal to This is from postman. With this in place, we now have our very first HTTP endpoint set up for the Todo REST API.

Now I haven't talked exactly about what REST is. We're going to talk about that later. For now, we're going to focus on creating these endpoints. The REST version will come up a little later when we start adding authentication.