API Design in Node.js, v5

Testing the CRUD Routes

Scott Moss
Netflix
API Design in Node.js, v5

Lesson Description

The "Testing the CRUD Routes" Lesson is part of the full, API Design in Node.js, v5 course featured in this preview video. Here's what you'd learn in this lesson:

Scott demonstrates using Postman to test creating, retrieving, and updating habits. He also shows how to structure JSON requests.

Preview
Close

Transcript from the "Testing the CRUD Routes" Lesson

[00:00:00]
>> Speaker 1: Before we move on to Error Handling, let's actually run them in Postman So, let's do that Let me make sure my server is running It is OK, so for the first one that we did I have so many tabs We have the Let's create a user habit and then we can try to get them and then we can try to update them So the first one for the create a user habit

[00:00:00]
Where is that one at Route.post Here we go So this will just be /api/habit post request, and we need to follow this schema here, so let's do that go to Postman, make a new one here, so habits change that to POST go to the body, go to raw go to JSON And according to our schema, I need a name, a description, a frequency, a target, and possible tag ID

[00:00:00]
So I'll say name for this habit is stretch I want to stretch and then description is optional, so I won't do that Frequency, I'll say daily Target count, I'll say 30 days So we got that Tag is optional I think I still have a token here, this token may not be good If not, we'll sign in and give it a try, so I'll send this

[00:00:00]
OK, oh, target count should be a string I did put string Let's look at the schema of the database though Target count is an integer, so let's fix that This is a number There we go, change that, send that again Habit created There it is, stretching with a target count of 30 Everything's good to go I'm gonna take this ID now so we can use this to update it later, but now let's try to get all the habits, so I'll change this to a GET

[00:00:00]
And run it with the exact, I don't have to change anything cause nothing's gonna get sent up And there we go I got our habit, just one So we got that one back, there's the stretch one And then now let's try to update this one, so I gotta put the ID here I'll change this to a PATCH So I'll change this to a PATCH and then what I want to update is the name

[00:00:00]
So I'll change the name to something more specific, so I'll say stretch hammies, so I'm gonna stretch my hamstrings So I'll do that and aha, we did get an error Error that we got was, let's see Habits set name frequency targets I got stretch Hey, what did it say Missing from class entry with table habit tags I would say in that case

[00:00:00]
It's something here So habit tag IDs ID Where, oh, I put habit tags should be habits That's what it was almost positive Yeah, that was it Cool So there we go, stretching hammies Good to go there Like those are the only three that we did, right, we did, yeah, like those are the only ones that we did All right, so any questions on those card routes

[00:00:00]
And just a note that you've conveniently included several different request payloads and everything in your repo The API docs Markdown file You got lots of like copy and pasteable Oh yeah, yeah, curls if people aren't using Postman or just JSON payloads and everything that they can copy-paste Yeah, I actually had AI generate this, so this was really nice

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now