API Design in Node.js, v3 Using Insomnia
This course has been updated! We now recommend you take the API Design in Node.js, v4 course.
Transcript from the "Using Insomnia" Lesson
>> Scott Moss: So now that we're on 3,000, I'm gonna go ahead and interact with our API. So to do that, there's a lot of ways you can do it. I mean, if you're just doing a whole bunch of GET requests, you can just go to the browser and just type those URLs in the search bar or whatever and just see what happens.
[00:00:14] But we're doing some post requests too and that's really hard to do from a URL bar. So normally what people will do is they'll use like some clients on their terminal, all different types of things. I've been using this tool called Insomnia, which is very much like Postman, if you ever used Postman before?
[00:00:31] Yeah, I use Insomnia, which I think is way better than Postman and it's free. So, that's what I'm gonna use in today's course to interact with the APIs that we will be making. And you can download that on the Internet. So if we just go to Insomnia, I shouldn't have Googled that.
[00:00:51] Why did I do that? [LAUGH] Insomnia api, there we go, Insomnia REST Client. There's a Mac one here. It looks like it works for Windows and Linux as well. So everyone should be able to download it if you want to. If you prefer, you can use whatever CLI tool that you want or Postman.
[00:01:09] Doesn't really matter. This is just how I interact with my APIs. I like it because it has support for GraphQL too, which is really cool. So I'm gonna head over to Insomnia. And this is what it looks like for me. I have a dark theme on. Yours might look different.
[00:01:23] Might be a light theme. Mine is a dark theme. And I'm just gonna go ahead and add a new route, a new request for our server. I'm just gonna say get data.
>> Scott Moss: So I'll create that. And then I'll add the URL for our server. Remember, I said it's gonna connect to localhost on that part that we pass in.
[00:01:46] So for us, it's gonna be localhost, I put in port 3000. So that'll be the url, and then the path that I want to hit is just gonna be right here, /root as in nothing. So it's just gonna be the entry point to the server. So the url is just gonna be exactly this, 3000.
[00:02:06] There's nothing after it. It's just that. So I'm gonna go ahead and run that request. And it messed up because I misspelled localhost. So I'm gonna run that again. And there you go. We get our message. Message, hello. Which is exactly what I put here, or here. So that's our GET request.
[00:02:29] And then for the post requests it's exactly the same thing so I'm just gonna copy this, I'm gonna duplicate it. And now I'm gonna change it to a post. And then I need to send some data to the API. So in this case in the Sami I can just post a body if you're using any other API tool to do a post request.
[00:02:55] It's however, they allow you to send json. This is where you would type in that json. So in this case I can put whatever I want. So I'm gonna change this to json. I'm gonna say message. Yeah. It's gonna do that then I'm gonna send it. So one thing to note is we got back a message that says okay and then we should see a log on a server that says message.
[00:03:20] Yo, and we do. So that's what we sent, this message yo. And we logged it right here on line 20. Rec.body. And then we responded back with just message ok which is what we got back here in the response. So it looks like our server's working. We were able to get some data and send some data.
[00:03:40] So if you followed along you should have this going, but what I wanna do now is just take a ten minute break and to give people time to actually experiment with this and go a little further, see if you can make some more routes that aren't just slash, maybe add some other stuff here.
[00:03:55] Some other stuff here, see if you can maybe even break something, just experiment with the express load of it. It's only gonna be ten minutes. And then we'll continue with our next lesson.