Check out a free preview of the full Introduction to MongoDB course
The "Build a REST API Exercise" Lesson is part of the full, Introduction to MongoDB course featured in this preview video. Here's what you'd learn in this lesson:
Students are instructed to write several controllers. Insomnia is introduced as a tool to construct http requests.
Transcript from the "Build a REST API Exercise" Lesson
[00:00:00]
>> Scott Moss: So the exercise you're gonna be doing is basically what we just did, but without having to write express. You won't have to write any express. So if you don't know express or know, don't worry about it [INAUDIBLE] any of it. But if you look inside the app folder, there's gonna be two files.
[00:00:12]
There's gonna be a todo.js, very similar to the node.js that I just made. And then it's gonna be an index file that's already got a express server built out. The only thing that's missing are these three controllers. These three controllers just expect to do three things. The get request to slash to do slash parameter ID is expecting to find a to do by the ID, and the ID is gonna be here.
[00:00:37]
So given this ID I want to go to find us to do by this ID. So it's just a basic find my ID core that you've done before, and they need to respond using the response object. You can look at the code that I did to see how to respond.
[00:00:48]
This next one is going to be getting all todos. So this one expects to just give me all the todos and respond. And then this last one is expecting to create a todo so, here's a to do that it expects you to create. It wants you to create it and send it back.
[00:01:01]
So exactly what I just showed you in my example. And that's it, there are no tests associated with this, so you just get those to work. And if you want to test them out you could use your browser for the get requests. You just literally just type in, when you run the server you'll see a address that you can go to that where your server is located.
[00:01:20]
So you just copy this address, localhost 4000, paste that into your browser and then just do the URL. So I'm gonna go ahead and run this to show you what I'm talking about. Let me do a consolt log on this one. So let me do a rest.sin, hello.
[00:01:36]
There we go. So if I run this example which is going to be node exercises/app/index.js. If I run that, I get an error because, yeah. You also got to pass in the connection string here, add mongo url here, to connect to the database. So I'll just go on ahead and hard code one right quick.
[00:02:04]
>> Scott Moss: Local host, course always 27017, and I'm just gonna say yo. Could be any database, so we'll connect there.
>> Scott Moss: So, you can see, it'll log this out Http://localhost:4000. You can copy that. You can go to your browser and paste that in. Then what I want to do is a /todo and /:Id.
[00:02:31]
So just ID of what todo,we don't have the todos yet and I'm just sending something back, so I'm just going to set that route. So /todo and then slash whatever, and you can see I get back hello. This is exactly what I put here. So your job is instead of sending back hello and hello world you're just gonna send back the result of your database query.
[00:02:52]
That's it, so you're only writing database queries here, connect to your database here, and then just respond and that's it. Any questions on that exercise?
>> Scott Moss: Yes.
>> Speaker 2: They could use postman to construct the post request.
>> Scott Moss: Yeah, so for the post request, there are a lot of options.
[00:03:13]
You can't really do it in a browser cuz you can't do a post request in the URL. So if you have postman, need to use postman, which is literally built just for this purpose. I usually got CLI tool, but recently I've been using something called Insomnia, which is very much like postman, it's a desktop app that allows you to construct http stuff.
[00:03:34]
So I can come in here, I could paste that same thing in here and be like todo, and then I could add,
>> Scott Moss: Not graph QL but I wanna add some JSON,
>> Scott Moss: Or JSON's kinda ridiculous to write. So yeah, I'll just write JSON. So I can add a JSON here, and then obviously my server's going to break cuz I don't have the solution up.
[00:04:01]
But if I had a todo here with a name of like do your work,
>> Scott Moss: Like that, I could just send this request and it would do a post request. It's gonna hang for me, because like I said I don't have the solution, so you can see it's just like breaking right now.
[00:04:20]
It's just killing it right now. So yeah, you can use this as well, but any HTTP client, API client that you wanna use, you can use it for a POST request. You can also do the forget request if you want, so that's how you would test these things.
[00:04:39]
>> Speaker 2: Is it free?
>> Scott Moss: It is free. There's a paid version for it. It's just one guy who opened source this and he's asking for donations, really good work actually. He does a lot of good work, actually. He does a lot of good work. But no, this is free, I haven't paid anything for it, but it's really nice.
[00:04:52]
You might get a little ad that pops up and is it hey, pay me, and it'll show his face for the sympathy.
>> [LAUGH]
>> Scott Moss: [LAUGH] So yeah, it's free.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops