RESTful API From Scratch Using Node, Express and MongoDB

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this video I'm going to show you how to build a restful web api service using nodejs and Express alright in this video we'll do the backend and then probably in another video we'll build a front-end using angular alright but I just want to focus on the backend service right now we want to be able to make requests to this application to get our data get and it's going to be a bookstore so we want book data and probably genres those will probably be the two models that we have alright so we want to be able to make get requests we'll want to make post requests to add data put requests so that we can update data that's already there and then delete requests ok so we're going to do this from scratch we're not going to use any kind of generator or scaffolding or anything like that I mean that stuff's great for when you need to get something up and running when you already know how to do it but if you're just learning that doesn't teach you very much because it just does it under the hood you don't even know what's going on so we're just going to create the files and folders from scratch and do it that way all right so let's go over the technologies we'll be using obviously no js' which is just JavaScript on the server it uses the same JavaScript engine that Google Chrome uses and it's really powerful alright to install it I'm in Windows so I'm just going to actually already have it installed but if I was I just click this button and just go through the installer all right and I'm if you're using Linux you can use your package managers or whatever you feel comfortable with all right and then Express is a web framework that has a web server that we can use to handle our requests it also has it can use template engines it's actually really powerful and to install this you're going to use NPM which comes with nodes so once you install node you'll be able to use NPM and I'll go over this in a minute okay so we're also going to use MongoDB as our data store I already have it installed but it's pretty easy to install if you're using Windows just click download go through the Installer I actually have a video that shows you step-by-step how to install on Windows so if you have any trouble just check that out now when we build the front-end which won't be in this video we'll be using angularjs which is really powerful client-side framework built on JavaScript I would definitely suggest learning angular if you don't know it it's really powerful and it's really popular now to interact with our database we're going to use something called Mongoose which is it's a node an NPM module so we'll be installing it through NPM and then for your command line you can use the standard window windows command line or powershell but i'm going to use this this program called git bash that comes with git for Windows I already have it installed but if you want just click on download for Windows and get that installed alright so let's get started I'm going to create a folder so I'm going to go on my C Drive and then projects and let's create a new folder and let's just call this we'll just call it bookstore alright now we're not going to actually have when it's not going to implement any kind of e-commerce what I'm going to do is we'll have a button that will point to the Barnes & Noble page that has the book because I don't want to get into e-commerce and all that I just want to just show you how to create that back-end restful api service alright so we have a folder called bookstore we're not going to need very very many files right now we just need a main app j/s file that's basically the gateway to our application and then since we're using Mongoose we're going to want to create models for our data so we're going to create a models folder alright now we're also going to need a package.json file and you can just go ahead and create it but you can actually use NPM to create it I'm going to open up my git bash actually I'm going to open it in the folder that I'm in so if I right click on book store I can say get bash here and that will take us to the directory all right now before I do that I already have Express installed but you want to go ahead and say actually no we're not going to you don't need to if you want to install it globally you can say NPM install G Express alright that will install it globally but we're actually going to install it locally and for this project so let's create our NPM I'm sorry our package dot JSON file so we're going to say NPM and knit alright and it's going to ask us a few questions today so the name of the application we'll just keep it as book store the version that's fine description it's a simple bookstore app entry point is going to be at Jas author feel free to put your own name license and it's going to ask us if this is okay okay which looks good all right now if we go into bookstore and I'm actually going to open this in my text editor alright and I'm using sublime text too I like it it's really powerful but it's also simple so let's open up bookstore and then your package.json file should be in there and what we're going to do here is we need to add our dependencies we need to specify which node modules that we want to use in this app so let's go ahead and say dependencies and that's going to be its own object and we're going to say Express alright and we want the latest version so we're just going to use an asterisk alright next we need something called body parser and that'll make it so that we can submit forms and make post requests and get the the values from the form and then we need Mongoose all right and then that's all we need for dependencies right now make sure you put a comma here as well all right so let's save that now to install these I'm going to go back to my git bash utility and we're just going to say NPM install all right so that's going to go ahead and install those three dependencies for us all right so now what we want to do is open up app KS and we're going to require the things that we need all right so we need Express so we'll say variable Express equals require and we want to pass in Express all right then we need an object to represent our Express application so we're going to save our app is equal to Express alright and we're also going to need body parser all right and then Mongoose okay so now what we want to do is connect to Mongoose so all we have to do is a mongoose dot connect and then we want to pass in the location of our database or the URI so we're going to say MongoDB localhost and we didn't create it yet but that's fine we can still specify it let's just call it bookstore all right and then we need a database object so say VAR DB and that's going to equal Mongoose dot connection all right so now we need to set up some kind of route all right so let's set up the route for the actual homepage or the landing page whatever you want to call it so to do that I'm going to say app get alright now this is how we handle requests since I said get that's going to handle a get request to whatever you are I put in here ok you can also have app dot post app dot put app dot delete any kind of HTTP request all right so we want to get for the slash which represents the home page and then we're going to run a function okay when someone visits that page we'll run this function and this takes in a request and response object and we can use that response object we can use a method called send which is basically just going to send to the browser whatever we put in here all right so let's just say hello world okay now before we can actually run our app we need to tell it where to run or we had to listen so we're going to say listen on port 3000 all right and then let's just do a console log so that we know we're connected okay we'll say start we'll say running on port 3000 all right and that should that should be all we need to actually be able to run our app so to run it we're just going to say node app what's this get of undefined what did I do app get all I have a dot here that's the sorry but that should be a semicolon all right so node app running on port 3000 so now if we go over here and go to localhost 3000 we get hello world okay now when people interact with the API I want them to have to go to slash API alright so it'll be like slash API books for the book model okay so when if they go to just localhost then I just want to have a message to say use the API endpoint alright so we'll say please use slash API slash books or slash API slash genre or genres okay so if we save that and reload we don't it still says hello world and that's because we have to restart the application so if we do control C and then run note app again now it's changed alright to get around that and not have to restart it we can install something called node mod alright so let's stop that and we're just going to say npm install g4 global node mod okay so now if we just say node Mon while we're in that directory it's running it alright so let's go ahead and reload and we didn't change anything so let's just we'll say I use one just to test it out and you can see it changed without having to restart the app alright now before we go any further let's uh let's add so let's create our database and add some data to it okay so if you have MongoDB installed then you have the the client alright so let me just find my installation I'm not exactly sure where it is think it's yeah it's in Program Files ok so wherever your stalled DB click on that and then you get server 3.0 and you want the bin directory so I'm going to get bash into that directory alright and then we'll run actually let's do slash dot huh that's weird let me try it in using the standard Windows command line because I think I've had problems with that before in that gate utility so let's say command to run as administrator okay then we going to navigate to that directory so I'm going to go to Program Files DB server 3.0 slash bin alright and it will say alright so now we're in the shell you can see that the prompt it's turned to a greater than sign alright so let's say show dbe's it's going to show us all the databases that we have if you just install MongoDB then you'll probably just have local alright so what we want to do is create a new database so all you have to do is say use and then the database name so it's going to be bookstore okay so now we're in the bookstore database and we can say show collections and we don't have any collections so let's create one okay so we'll say DB dot create collection and we're going to pass in books okay so we get an okay and now if we say show collections again you can see we have books as well as system indexes but just ignore that all right so let's create another collection called genres okay so now we should have two collections books and genres let's go ahead and open up the Barnes & Noble website and they just want to get some examples alright so let's see we have the little Riley or do I want let's look at this one murder house alright so this is a suspense so let's create a suspense John rrah so do that we're going to say DB dot genres dot insert okay we want to insert a JSON object so let's give it a name now the ID will automatically be generated so we don't have to worry about that let's say name suspense I think that's all we need really for suspense I mean I'm for genre okay so it says that that was inserted now we can say DB dot find I'm sorry DV dot John res dot find and you can see we have one result and it has an ID which is an object ID and then the name let's go ahead and add one more all right so this one let's call this one self-help okay so now we should have to shun Rah's all right now let's add a book okay so to do that we're going to change this to DB dot books dot insert all right so let's insert title the murder house and so let's see what else do we want we want the genre so for that will put suspense alright next we're going to need description alright hands let's see it's just copy this all right and then we'll go ahead and paste that in and get rid of that : okay so we have a description next thing let's do the author which is James Patterson oops what I do James ah all right James Patterson all right next thing is going to be the publisher so the publisher let's see say overview publisher a little brown and company say Brown and company all right let's see pages let's do that so pages it has what 480 and let's do now I want to do images but I don't want to have to upload them we're just going to have an image URL field alright so we'll say image that image underscore URL and let's go ahead and get the URL for this image we'll say copy image URL and we want to paste that right in there alright next we're going to want the by URL okay so this page alright so I'm just going to copy this and we'll go ahead and paste that in there and then let's see I think that's good let's try and run that we may have an error no I went in good so now let's do DB got books dot fine all right good so now let's add one more so say we'll click on this truth or die which is also by James Patterson alright we're just going to just want to click the up arrow so we can get back to that query and then we'll just change some things all right so let's go to the beginning so we can change the title I'm going to go ahead and delete this description too okay so descriptions blank and let's change the title actually the genre can stay the same the title change that to truth or die okay in the description let's go ahead and copy that from here all right so paste that in is that right yeah it looks right I think we get the comma all right so author we can keep that let's change the going to have to change the image URL and the by URL so I'm going to go to the end here delete this okay and I will delete this one hope you guys can see that whoops what I do all right so the image URL I'm going to get from here okay paste that in and then the by URL is this okay and so let's see now it looks like this quotes are an apostrophe in the description and that might mess things up doing it this way through the shell so I'm just going to delete that right here he's has one and when we do if we add this through our application through the front end it's not going to be like this it's just because we're in the shell all right so yeah that should be good so let's try that alright so that was inserted now we'll do DB books dot find and you can also add on to this dot pretty and that will format it a little better so now you can see clearly that we have two two different books all right good so now we can go back to our app J s file let me minimize this we can go back here and we're going to have to create a route what we want to be able to do is go to slash API slash let's do let's do the genres first so slash honorees and that should give us a response in JSON format with the genres all right so let's go add that route will say app don't get all right and then here we're going to put the URL we want which is going to be slash API slash genres so when they make a get request to this URL we're going to run this function and this needs to take in a request and a response alright now before we can actually do this I probably should have done this earlier we need to create our models alright so let's save that and then go into the models folder and create a new file and save it as book dot J s and we also want John Raj is all right so we'll start with genre we're going to save our Mongoose we want to require it alright so we're going to create a schema for our onerous alright so this is an a schema isn't required for the actual database this is just for the application alright so we're going to save our genre schema is going to equal a mongoose dot schema alright and i'm going to pass into that an object with all the fields that we want alright excluding the ID because that's automatically generated I mean you could override it if you want but we're just going to leave that okay so John row will have a name alright so the type of field this is is going to be a string and we want it require it as well so you can do validation from here as well all right and then the only other thing that I want is a create date alright so this is just going to be whatever the date and time is when it's submitted alright so type is going to be date and we want it automatically inserted so we need to say default is going to be date dot now all right that will put that specific date and then down here I'm going to create a variable called genre and we set it to module dot exports this is going to make it so that this genre object is accessible from anywhere else alright notice that equals Mongoose dot model and then we want to pass in genre and also the schema which is under genre schema okay so now this object can be accessed from outside now you could do all your database finds and inserts and all that we could do that right from the route if we wanted to but I like to keep it all encapsulated in them in the model alright so we'll have our function here to get genres looks I want to do that just want Shawn Rah's oh I don't want that all right so let's say mod now this needs to be accessible from outside so module exports and then the function name which will be get genres all right so actually this should be equals function all right and this function is going to take in the callback which will will enter through the route file and then optional limit okay and then inside here we're going to say genre genre don't find ok just like we would in the shell and then that's going to take in a callback and then the optional limit so we'll say dot limit and then pass in whatever comes from the parameters and that's it alright so that should get John Rah's so we should now be able to access this function from the route so let's go back to the route and before we do anything here we have to include that file all right so we're going to go up here and say variable genres equals I'm sorry no we don't want to do that I'm going to say genre with a capital G is going to eat grow require and this is in the models folder so we'll do dot dot slash models slash genre dot J s actually I don't think we need the Dodge a s so now in the row we should be able to access that genre object and all its properties and functions so we'll say genre dot get genres alright and then here we're going to pass in that callback which is just a function and that function is going to take in an error if there's an error and then it'll give us not customized on race all right and then we can say if there is an error then let's just just say throw her okay if there's not then we're going to say resonates on because that's the format we want to send and then we'll just pass in genres which comes from here alright so we'll save that and let's see what happens okay nothing Oh thrice that's right oh no it's not genres there we go so there's our two genres okay so we have an ID we have the name and then the create date and if you remember we didn't even put this in when we added it through the shell that is just new to add it alright so now we want to be able to get the books so let's go ahead and create the book model I'm actually going to copy everything from the genre model because it's structured the same way we're just going to have extra fields so book schema ok we're going to have actually let's change all this first okay and then up here let's see all right so books chemo we're not going to have a name it's going to be a title type string required and then we're going to need we're going to keep the create date so let's just add to this genre since a comma now let's make this same thing string and required and then we'll just copy that okay the next thing is going to be a description and I guess we don't have to require that let's see author publisher let's make that not required the pages and this could be a string or number let's just let's just keep it a string and not required all right and then I'm going to have an image URL and a bi URL and that's it all right so that's those are the properties now for the methods let's say get books okay and then it's going to be the same thing just change this to book all right so now if we go back to our app file let's copy this and we're going to change this to API slash books and then we're going to call book dot get books change this right here two books and this all right now it's not going to work now because we didn't have we don't have the book object so we're going to copy that all right so let's see if that works if we go to API slash books there we go it's going to return all the books okay so now what we want to do is we want to be able to get a single book so what we want the URI to be is API slash books slash and then whatever the ID is all right so let's go ahead and we'll first write the model function so in the book model let's copy this and we'll say just get book that's what we'll call this as well get book all right and this one actually know what let's change this to get book by ID all right and then the function is going to take in a book and a callback we can get rid of limit because it's only returning one all right and then down here we can use book dot find by ID which is a mongoose method all right and then that's going to take in book actually you know what let's change that to ID as well as this all right I'm going to take that limit off and that should do it all right so now we'll go back to apt Jas let's copy this all right now it's going to be API slash book slash and then colon ID all right so that's going to represent whatever the idea is that's passed into the URI all right when I see URI and URL I mean the same thing all right so this is going to be get book by ID and that's going to take in the ID that comes from the URI all right so to get that it's going to be a request quest dot params dot ID actually you know what let's do underscore ID here and here and then that's going to be the same and it's going to return a single book so we want to get rid of that s same thing here alright so we'll save that now let's go ahead and grab the ID of the murder house and we're going to put that in right here and then you can see that that now it's giving us just that one book so now we can get John Rizzo all the books we can get one book so the next thing we want to do is want to be able to add a book so let's go back to the model actually know what let's do the genre first because that's a lot easier alright so we want to add genre alright so what we're going to do is let's copy this I'm going to change this to ads on rrah all right and that's going to take in let's get rid of this limit that's going to take in a genre object okay which is going to be data from the form and then let's see we're going to say genre dot create and then we'll pass in that object all right and we want to get rid of this limit let's save that and then we'll go to app J s and let's go up here after the get genres copy that and we're going to change this now to app post because it's what to taking care of a post request and it's going to be the same URL alright so it doesn't matter if you have the same URLs as long as the request is different all right we should we could also have a put in the delete for the same URL as well all right so what we're going to do now is let's go right here I'm going to save our genre is going to be equal to request dot body and this is where body posture comes in so this is going to allow us to access everything that comes in through the form and we're going to put it into a genre object sorry about that so then now we're going to see genre dot ads onra alright and then we're going to also pass in that object and then we have our callback change this to just yon rrah and then same thing we're just going to respond with genre all right so let's save that now in order to test this we're going to have to make a post request so there's a lot of different ways you can do that one is with rest easy which is a I think there's a chrome a chrome app for that I don't know if I have it in this particular machine don't think I do see rest easy crow all right so add this to Chrome okay so now let's open that now we should we should be able to let's see we want localhost API slash genres all right now it's set to get by default and we can actually run that and you can see it gives us a 200 ok status and gives us what we return it gives us all the genres all right so to make a post request we're going to change that to post all right and then sorry about that let's see query parameters we have the headers okay so for our headers we're going to add in the content type and that's going to be application slash Jason all right and then in the body we're going to send an object and for this you have to have quotes around your key as well so let's send name and we'll say romance and that's pretty much it I think yeah that's all we want to be able to add so let's try that out okay it looks like it's it's hung up on something I know what it is it's the middle way for the body parser alright so we want to go back to app KS and go to the top and we're going to go right here and we're going to say app dot use this is just the it's just one line of middleware just to kind of initialize the body parser alright so app dot use body parser and we want we're using JSON so body posture dot Jason alright so that should fix it so let's go back and try again actually we get our I wasn't using node Mon that time so I got a restart I might have to reload this whole page okay good there we go status 200 and it gives us gives us that row alright so let's go check it out so in the shell will do DV John resigned and now we have romance and also it added the create date cool so now we want to be able to do the same thing with books so let's go to let's see we'll copy this from the genre model and then paste it in the book model change all this okay I'm going to pass in a book save that okay now we'll go back to the app J s file and let's copy the post that we did for the genre and then we'll just change it to post to API books all right so book now what I'm doing here is just to show you how things work this would not be used on production it's not it's not very safe to just have whatever someone adds and while they make a request to go into your database all right we do have Mongoose which uses models which makes it a little more secure but you want to kind of dissect the fields and insert them separate individually and you also want some kind of authentication so there's a lot of security measures to take if you're going to be or if you're building a production app okay this is going to be at book all right that looks good save that all right so let me restart this I'm going to switch to use no daman all right so now what we're going to do is let's change this to books and Post okay so we're going to add some things here let's change this to title and let's go back to Barnes & Noble all right let's see subjects will get a romance novel okay it looks good on actually let's get the notebook alright so I'll grab this description okay title and then we're going to have the genre which will be romance description that in let's see what else do we need the author Nicholas Sparks okay publisher say fubar what else did we have pages I'm just going to say 400 and then we need the image URL whoops 20 quotes image URL let's see copy link address all right and then we need the buy URL alright I think that's everything let's check description publish our pages yeah that looks good all right so let's go ahead and try to send this to API slash books okay so it gave us the object back now let's go verify so we want DB dot books dot find dot pretty and there it is the notebook okay so now we can we can get data we can can post data now we want to be able to update data all right so let's go to we'll start with genres so we'll go to the genre model alright so this is going to be a little different because we actually have to take apart the DS the object being submitted so let's change this to update genre change this okay and let's see function it's going to take in an ID it's going to take in the new genre object and then it's going to take options and then a callback all right and what we want to do let's create a query okay we're just going to set that to underscore ID ID all right and then what we're going to do is create an object called update and we're going to say name name will be genre dot knee so we actually have to assign each field this the genres model only has one field so it's easy but we'll have to add a bunch of these for the book alright and I'm going to say genre dot and then Mongoose has a function called find one and update and it takes in a query which we wrote out right here so query then it's going to take in the update and then options all right so that's the model function now let's go back to app guess and let's get just copy the post actually we're in genre so we'll copy the genres post and then I'm going to change post to put now put will have to take in an ID so it's going to be API genres and then ID alright and then let's see you'll say var ID equals request dot params dot let's change it to an underscore ID you don't have to but just because that's what it is it's in the database as alright so we get the ID genre is still going to get the body so we need that and then we're going to say genre dot update genre and we want to pass in the ID pass in the genre for options we're just going to leave it blank but we still need to put our blank curly braces and the rest should be fine so we'll save that and let's see if we can do this so we'll go back here we're going to want to grab a genre ID so let's go to API genres and let's grab this okay so this is for suspense so we'll copy that and then go back to rest rest easy and we're going to change it to API slash genres slash and then the ID I want to change this to be put and then let's change this say name and let's change it to thriller okay so that should work doesn't mean it's gonna all right so I'm not sure if it did work let's go check so we'll say DB dots genres dot find and it's it look named thriller alright so for some reason when it gives us the thing back it's still suspense but I mean that's we don't really need that to do it doesn't really matter for us so it did change and it did do the update so let's do the same thing to the books so I'm going to copy this put request here and I'm going to go down here and say app dot put and API slash books slash ID let's change that to look and then we'll say book dot update book which you're still going to create change that okay we want to change this and this okay we'll save that and then we'll go to the book model and let's grab let's copy the update genre paste that in book okay it's going to take an ID it's going to take a book object and query update going to change this to title okay we're going to need all the rest of them as well so title and then we have genre whoopsie should all be book all right so we got title and then genre which is going to be equal to book genre description what else do we got author publisher let's see what else we need our urls so we have the image URL oops and then we have the by URL Oh pages to I think that's it alright then we're going to change this to book dot find one an update I'm going to actually the rest of that it's okay all right so save that and then we'll go over here we need to get an idea of a book so same thing let's just go back to our app our API and say books okay we'll take the martyr house take that ID and then we're going to go API slash books slash and then the ID put and let's change the title okay so we know it's the murder house let's just change it to murder house send and let's go check it out so genres dot not not genres books all right so let's see title Murder House okay now what happened here is everything else went No and the reason for that is because we didn't include it here so usually if you would submit from a form on a website on a web page then you have the form prefilled already when you go to the Edit form so it would all submit here we're just submitting raw JSON data and I didn't include everything else so it just kind of over wrote the whole thing and you can use different types of queries like we could have used I think it's set and that would have just updated just this field but I'm not going to really worry about that okay so next we're going to do deletes so we'll just delete this book anyway all right so let's do that so we'll start with genres so we'll go to the genre model let's just copy this say wheat okay change this to delete and we're just going to call John rIDOT remove and that'll take actually we need to set the query so I'm going to copy what we did here and update and then we're just going to pass in query all right so now we'll go back to app let's copy this okay same thing it's going to be a app dot delete same you I'll I will get the ID we don't need this because we're not getting we're not posting any data so we can get rid of that and then I'm going to say book dots remove book is that what I called it oh I'm sorry we're doing we're doing genre so this will be John right okay and then that's going to be genre dot remove genre and it's just going to take an ID and a callback all right and then we're just going to return genre alright let me just check that model well quick delete genre okay alright so let's try that so we need we need an ID okay so let's get rid of we'll get rid of self-help since there's no books in that in that genre so we'll copy the ID let's go back to rest easy and we want API slash genres slash the ID I'm going to change this to delete get rid of this body and send okay so we got 500 hours on rIDOT remove genre is not a function ah let's see oh we have delete John or not remove all right let's change this to remove and we'll try it again I D is not defined I'm going to change this to ID all right so looks like it worked let's check it John Roos dot find and we only have thriller and romance perfect okay so the last thing we're going to do before this is complete is we need to remove the book okay so let's go to the model and just copy this genre delete and do the same thing with the books now this is something that you could use for multiple programming languages all right so as long as you can make HTTP requests from whatever language your framework or whatever you're using then you can use this API all right so save that go to app let's copy this actually the genres the genres delete should be up here so I'm just going to put this up here and then we'll change this one okay so be removed book all right save that let's go get an idea of a book okay let's do we'll get rid of Murder House because it's all null anyway grab that ID okay then we'll go over to rest easy and let's change this to API books / ID delete send and that should be gone let's go back to this page you reload and it's gone all right so we now have whoops spelt that wrong all right genres so we now have a complete restful api all right now obviously it doesn't have to be a bookstore you can use whatever data you'd like it can be as simple or as intricate as you'd like as far as the structure of the JSON document you can use arrays you can use embedded data but this is a good solid start to to building a restful api so hopefully you guys enjoyed this hopefully you'll learn something from it if you liked the video please give a like subscribe whatever you can do and in the next video we'll be making a front-end to interact with this API alright thanks guys
Info
Channel: Traversy Media
Views: 440,227
Rating: undefined out of 5
Keywords: RESTful, REST, REST JSON, RESTful api, express api, http requests, nodejs json api, nodejs express, mongoose express
Id: eB9Fq9I5ocs
Channel Id: undefined
Length: 65min 58sec (3958 seconds)
Published: Fri Oct 02 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.