API Design in Node.js, v4

Testing the API with Thunder Client

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 "Testing the API with Thunder Client" 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 demonstrates using the VSCode extension Thunder Client to test API endpoints. An endpoint needs to return something. Otherwise, the request will hang. With HTTP, the connection with the server is closed once something is returned.


Transcript from the "Testing the API with Thunder Client" Lesson

>> Okay, cool, so now, we should be able to try this, but we're gonna run into an issue, so let's observe that issue. So I'm going to, actually I don't remember I even started my services I did TypeScript, so, [LAUGH] I don't know if this is gonna work.

Let's do, what is yeah node, I think I gave it a command and I dev, yeah, there it was. It was dev, so let's do npm run dev, it works, you just gotta love it works, so we got that, and then now, what I'm gonna do is, I'm going to introduce you to a really cool VS Code plugin.

So we don't need to use the browser anyway, if you're using VS Code that is if you're not, what are you using? And let me know why because maybe it's better I don't know, but VS Code plug in is called thunder client, how they were going to download it, in fact, encouraging you to download it for this course.

I will be using it exclusively to test out our API calls, thunder client is just like any other HTTP gooey, it's just a place where you can go test out API calls. We can only go so far in the browser because the browser only allows you to do, get requests when you hit enter the URL.

What if you want to do a put or a delete, or a post or a patch, you can't, you gotta write JavaScript for that. But who wants to write JavaScript for that, we should be able to click on things, okay thunder client, lets us click on things, so I'm gonna go in here.

You can make a new request, I'm gonna do a get request to my localhost, my port/API/product, okay? I got some other stuff in here, don't worry about this, we'll get to that later, I don't want anything in the body because it's a get request, I'm not sending anything, and I'm going to run this.

But we're gonna run into a problem, so make sure your server's on, that way I hit that, so right now, you can see that it just says processing, processing, processing, okay? This should not happen, okay? It's not thunder client that's broken, it's my API, my API is doing what's called hanging, it's the equivalent of you trying to call someone, and they just don't pick up.

Or no, actually, I think it's more like they did pick up, but then they didn't say anything, [LAUGH] he's just like, you can just hear them breathing on the phone, but they don't actually say anything. And then, eventually you get tired, and you hang up, that's what's happening right now, so our server picked up, it was like yo, I got you, and then it just continued to just mouth breathe on the phone like forever.

And then, you couldn't actually understand what they were saying, so, We need to fix that, so what I'm gonna do is just go into our code, okay, there we go, I forgot this thing was there. And for the product one, I'm just gonna just put something in here, I'm gonna say request response, and I'm just gonna say res.json, put a message in here.

Hello, like that, you could send back whatever you want, I'm just gonna send back something here for the get on the product, I'm gonna say back hello, I'm gonna hit Save. This thing isn't auto restarting, so I'm gonna stop it and restarted again, I'm gonna go back into thunder client, and then now, go to issue the request, and then you could see immediately I got something back.

Status 200,19 bytes 15 milliseconds, so our API route is working, we can go to /API/products, and it works. You try to go somewhere else, we try to do a post here, this is gonna hang, we don't have anything, right? You try to do Delete, that's gonna hang, we don't have anything, and we'll fix that, we'll get to that in a minute, but we got the routes.

Any questions on that?
>> I mean, these are very generic corporations, you declared, do we have any skip holding option where we say this is a product and not generate all the ropes?
>> Yeah, so you talked about like rails or something?
>> Yeah.
>> Really can kinda generate all this stuff?

>> Okay.
>> Yeah, there's nothing built into node or express that allows you to generate anything, however, I can remember was v2 or v3, I did write it in that version. I think I did generate the routes automatically based off like a configuration file, so you can do it yourself.

There's also CLI and tools that tried to like simulate what rails did, there was one that was called sales a long time ago, I think it's dead now, that was trying to be like rails for express and it kind of did that. But there's also like really good, like CLI and stuff that will just like to generate it based off of like, a Mongo model or a Postgres model like, or it'll literally connect to your database.

And like, I see all the stuff on your database, here are the routes, it'll do that, but there's nothing baked into node that does that. And in fact, that's the thing with node, it is like it actually doesn't have anything because it just relies on the community. Which can be good or bad if the community depends on how, well, the community behaves.

>> Is the reason why we had to restart our server because TypeScript has to compile?
>> It's a good question. So the reason we have to restart our server is because we're not watching for any changes, ts node or even node in general, by default don't watch for changes.

So you can hit save and nothing's gonna happen, we're so used to like next js and things like that, that like we expect that to happen, but let me tell you it was 2000. I don't know, between 2013 to 2016, 15, you didn't get auto reload, like that was brand new, like you just had to do it yourself, but there's definitely a tool we can use.

I think node MON is probably the best tool that allows you did like restart your server, you can hook it up with TypeScript, I just didn't wanna get caught up with that. So I decided to actually, I had it, but I took it out I was like, nah, I think this is too much, but yeah, you just need something that watches your files, and then triggers a command when they change.

So, good question. Okay, but yeah, so we have that our stuff is hanging now cuz we don't have any handler, a server should never hang, that is a very, very bad thing. Yeah, you just don't want that happening, a server also cannot respond twice, so you cannot respond twice to the same request.

If you do, that will cause an error, just like you cannot say bye, hang up and then pick up the phone and say bye again, it doesn't work that way, like once you say bye, it's done, okay? So remember those two things, you can't respond twice, and you should always respond once, otherwise, bad things will happen, okay?

At least for HTTP, you start getting into Web Sockets is different, but for this protocol, yes, all right? Now that we got all that hooked up, basically what we wanna do is start getting ready for actually creating out with the flow of hitting this route, triggering some route handler.

And then, it targets to the database, but before we do that, we got to understand a new concept called middleware.

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