API Design in Node.js, v4

Applying Product Route Handlers

Scott Moss

Scott Moss

Superfilter AI
API Design in Node.js, v4

Check out a free preview of the full API Design in Node.js, v4 course

The "Applying Product Route Handlers" Lesson is part of the full, API Design in Node.js, v4 course featured in this preview video. Here's what you'd learn in this lesson:

Scott adds the product route handlers to the router and tests the application with Thunder Client.


Transcript from the "Applying Product Route Handlers" Lesson

>> Let's just add one or a few of these and see what happens. So I'm gonna go back to the router, go to the product. We don't have any products, so even if I get these, nothing's gonna come back. But let's just do it anyway, just to make sure it actually triggers it.

So get rid of that in line one, and I'm just gonna say, getOneProduct like that. That's it, so I have getOneProduct. I'm going to start and stop my server. Gonna go back to Thunder Client, hopefully I still have my API keys in there. Let's see, I do. And then I'm gonna try to get one product with an ID that I know doesn't exist.

Change this to GET, also I'm gonna get all products, I believe, that's the one we just added the handler two, all products. Why did I put getOne, it should we getMany, getProducts, or there we go, getProducts. So getProducts and I get one. Restart my server, then I can go back here, try to get all the products, make sure my API key is in there.

Try to run this, and you can see nothing failed. We satisfied the authentication by making sure there was API key. We satisfied any input, which for a GET request, there was none, so that was good. And then the handler, indeed ,tried to query the database. It just got back into your array because we don't have any products in the database, right?

And again, we can check that, we can see what's in the database if we just type out, npx prisma studio, you can see what's in the database. So right now, I can see that I have two users. I have zero products, so that's why I got back empty array for products, there are none here.

I can go in here and I can make one. If I make a product, I can fill this stuff out and it'll show me a product, but I'm not doing it here, that's gross. Cool, let's add another one. I always think this Thunder Client is another app, so I tab away from it.

[LAUGH] But it's in VS code and it's messing me up. I need to put it in another window or something. Okay, let's do, Let's go back to the router, okay. So let's actually create one then, since I could do the get one and update and nothing's actually gonna happen, cuz none exists, but we can do create.

So let's add that. Let's get rid of this generic one here, and we'll say create, Product, there we go, createProduct. So let's look at this chain right here. So on a post/product, check to see that they actually posted a body object with the name field on it that's a string, and then check to make sure that that's true, and then run the handler, right?

And don't forget that there's an authentication middleware running in front of all of these on server here. So all that's happening before it gets to the handler. And when it finally gets to the handler, we can just focus on the business logic. Okay, let's try that. I'm gonna run that, and I'm gonna go to Thunder Client here.

I'm just gonna say POST to API product, I have my bearer. So if I try to post right now with nothing in the body, let's just see what happens. Obviously, we can't, that fails the validation of the input, just checking to see if that works. But now, we can say, yeah, I do want a name.

And my product's gonna be called, Note Stuff app. So I can send that up. And voila, I get back a product. I created a product in the database. I put everything on the data field, so it looks nice. And you can see, here's the product ID that it was created at.

Here's the name of it, and here's who it belongs to, right? And if you wanted to visualize that, again, you can do the npx Prisma Dtudio. And you can go back here, and you'll see there's a product and a database.

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