Skip to main content
Loading Explore guides…
Node.js Express API · Acedra
Home
Explore
Computer Science
Node.js Express API
Sign in to save
Chat
If YouTube asks you to sign in, open the video on YouTube.
Watch on YouTube
Reload player
Transcript
160 segments
Copy
Hide video
0:01
hey guys in this video I'm going to show you how to build a restful web API service using node.js and express all right um in this video we'll do the back end and then probably in another video we'll build a front end uh using angular all right but I just want to focus on the backend service right now we want to be able to make um requests to this application to get our data get and it's going to be a bookstore so we want book data um and probably genres those will
0:32
probably be the two models that we have all right so we'll want to be able to make get requests we'll want to make post requests to add data um put requests so that we can update data that's already there and then delete requests okay so we're going to do this from scratch we're not going to use any kind of generator or um 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
1:00
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 uh obviously node.js which is just JavaScript on the server uh it uses the the same JavaScript engine that Google Chrome uses and it's really powerful all right to install it I'm in Windows so
1:29
I'm just going to uh actually already have it installed but if I was I'd just click this button and just go through the installer all right and um 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 um to handle our requests uh it also has it can use template engines it's actually really powerful um and to install this you're going to use npm which comes with node
2:00
so once you install node you'll be able to use npm and I'll go go over this in a minute okay so we're also going to use mongodb as our data store um 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 uh which won't be in this video uh we'll be using
2:33
angularjs which is a really powerful client side framework built on JavaScript um I would definitely suggest learning angular if you don't know it uh it's really powerful and it's really popular now to interact with our database we're going to use something called which is uh it's a node an npm module so we'll be installing it through npm and then uh for your command line you can use the standard window Windows command line or Powershell uh but I'm going to use this this program called
3:07
git bash that comes with Git for Windows um I already have it installed but if you want just click on download for Windows and get that installed all right so let's get started I'm going to create a folder uh so I'm going to go to 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 all right now we're not going to actually have it's not going to implement any kind of e-commerce what I'm going to do is we'll have a button
3:42
that will point to the Barons & Noble um page that has the book because I don't want to get into e-commerce and all that I just want to um just show you how to create that backend restful API service all right so we have a folder called bookstore um we're not going to need very very many files right now we just need a main appjs file that's basically the gateway to our application um and then since we're using Mongoose we're going to want to create models for our data so we're
4:15
going to create a models folder all right now we're also going to need a package.json file and you can just go ahead and create it but um 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 uh rightclick on bookstore I can say get bash here and that'll 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 um actually no we're not going to
4:47
you don't need if you want to install it globally you can say npm install G Express all right that'll install it globally um but we're actually going to install it locally for this project so let's create our npm I'm sorry our package.json file so we want to say npm and nit all right and it's going to ask us a few questions say so the name of the application we'll just keep it as bookstore the version that's fine description we'll say simple bookstore app entry point it's going to be
5:27
appjs 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 all right and I'm using Sublime Text 2 uh I like it it's really powerful but it's also simple uh 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
6:04
node modules that we want to use in this app so let's go ahead and say dependencies and uh that's going to be its own object and we're going to say Express all right and we want the latest version so we're just going to use an asterisk all right next we need something called body parser and that'll make it so that we can submit forms um and make post requests and and get the the values from the form and then we need
6:48
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 appjs 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
7:29
and we want to pass in express all right then we need an object to represent um our express application so we're going to say VAR app is equal to express all right and we're also going to need body
7:54
parser all right and then mongus
8:07
okay so now what we want to do is connect to
8:16
so all we have to do is say doc connect and then we want to pass in the location of our database or the URI so we're going to say mongodb
8:32
Local Host and we didn't create it yet but that's fine we can still specify it um let's just call it
8:42
bookstore all right and then we need a database object so we'll say V DB and that's going to equal mongus do connection all right so now we need to set up uh some kind of route all right so let's set up the route for for the actual homepage or the landing page whatever you want to call it so to do that we're going to say app.get all right now this is how we handle requests since I said doget that's going to handle a get request to whatever URI I put in here okay uh you
9:18
can also have app. poost uh app. put app. delete any kind of HTTP request all right so we want to get um for the slash which represents the homepage 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 um 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
9:54
world okay now before we can actually run our app we need to tell it where to run or where 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 uh start we'll say
10:19
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 uh what's this get of undefined what did I do app.get oh I have a DOT here that's sorry that should be a semicolon all right so node app running on Port 3000 so now if we go over here and go to Local Host 3000 we get Hello World okay now when people interact with with the API I want them to have to go to slash API all right so it'll be like
11:04
slash API books for the book model okay so when they if they go to Just Local Host then I just want to have a message to say use the API uh endpoint all right so we'll say please use SL API SL books or SL 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 contrl c and then run node app again now it's changed all right to get around that and not have to restart it we can install
11:53
something called node modon all right so let's stop that and we're just going to say npm install G for Global um node
12:13
modon okay so now if we just say node modon while we're in that directory it's running it all right so let's go ahead and reload and we didn't change anything so let's just we'll say uh use one just to test it out and you can see it changed without having to restart the app all right now before we go any further Let's uh let's add some let's create our database and add some data to it okay so if you have mongodb installed then you have the uh the client all right
12:53
so let me just find my installation I'm not exactly sure where it is I think it's yeah it's in program files okay so wherever you installed DB click on that and then you get server 3.0 and then you want the bin directory so I'm going to get bash into that directory all right and then we'll run uh actually let's do slash.
13:23
huh that's weird
13:30
let me try it in using the standard Windows command line because I think I've had problems with that before in that git utility so let's see command let's do run as administrator okay then we got to navigate to that directory so I'm going to go to program files mongod DB server 3.0 bin all right and then we'll say all right so now we're in the Shell you can see that the prompt it's turned to a uh greater than sign all right so let's say show DBS okay it's going to show us all the databases that
14:18
we have uh if you just installed mongod DB then you'll probably just have local all right so what we want to do is create a new database so all we 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 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 dbcreate collection and we're going to pass in books okay so we get get an okay and now
15:00
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
15:26
website and they just want to get some examples all right so let's see we have Phil O'Reilly what do I want let's look at this one murder house all right so this is a suspense so let's create a suspense genre so to do that we're going to say um db. genres. insert okay we want to insert a uh 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 uh let's say name suspense and I think that's all we need
16:19
really for suspense I mean I'm for a genre okay so it says that that was inserted now we can say DB .find I'm sorry db. genres doind and you can see we have um 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 selfhelp okay so now we should have two genres all right now let's add a book okay so to do that I'm going to change this to db. books. insert all right so let's Insert
17:18
Title the murder house and let's see what else do we want we want the genre
17:32
so for that we'll put suspense all right next we're going to need uh
17:44
description all right and let's see let's just copy this all right and then we'll go ahead and paste that
18:04
in and get rid of that colon okay so we have a description next thing let's do the
18:14
author which is James [Music] Patterson oops what did I do
18:25
James ah all right James Patterson all right next thing is going to be the
18:40
publisher so the publisher let's see does it
18:49
say Okay overview publisher uh Little Brown and Company just say brown and Company all right uh let's see Pages let's do that so Pages it has what
19:16
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 all right so we'll say image D image uncore 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 all right next we're going to want the buy URL okay so this page all right so I'm just going to copy this
20:00
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 nope it went in good so now let's do db. books. find all right good so now let's add one more so we'll say We'll click on this Truth Or Die which is also by James Patterson all right and we're just going to just going 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
20:45
title so I'm going to go ahead and delete this description
20:56
too okay so descriptions blank and let's change the title uh 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
21:29
all right so paste that
21:35
in is that right yeah that looks right I think we get the
21:46
comma all right so author we can keep that let's change the we're going to have to change the image URL and the buy URL so I'm going to go to the end here delete
22:04
this okay and then we'll delete this one hope you guys can see that oops what did I do
22:18
ah all right so the image URL I'm going to get from here okay we'll paste that in and then the buy
22:34
URL is
22:40
this okay and let's see now there looks like there's a a quote or 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 this
23:01
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 cuz we're in the Shell all right so yeah that should be good so let's try that all right so that was inserted now we'll do db. books. find and you can also add on to this pretty and that'll 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 appjs file let me minimize
23:41
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 uh Let's do let's do the genres first so slash genres and that should give us a response in Json format with the genres all right so let's go add that route we'll say app.get all right and then in here we're going to put the URL we want which is going to be SL API slash genres so when they make a get request to this URL we're going to run this
24:27
function and this needs to take in a request and a response all right now before we can actually do this um I probably should have done this earlier we need to create our Models All right so let's save that and then go into the models folder and create a new file and save it as book.jpg
25:00
all right so we'll start with genre we're going to say VAR mongus we want to require
25:12
it all right so we're going to create uh schema for our
25:21
genres all right so this isn't a schema isn't required for the actual database um this is just for the application all right so we're going to say
25:34
VAR genre schema is going to equal a do schema all right and we're going to pass into that an object with all the fields that we want all right excluding the ID because that's automatically generated I mean you could overwrite it if you want but we're just going to leave that okay so genre will have a name all right so the type of field this is is going to be a string and we want it required as well so you can do validation from here as well all right and then the only other
26:15
thing that I want is a create
26:22
date all right so this is just going to be whatever the date and time is uh when submitted all right so type is going to be date and we want it um automatically inserted so we need to say default is going to be date. now all right that'll put that specific date and then down here we're going to create a variable called genre and we going to set it to module. exports this is going to make it so that this genre object is accessible from anywhere else all right and we going to say equals do
27:07
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 the in the um model all right so we'll have our function here to get
27:45
genres oops don't want to do that I just want genres oh I don't want that all right so let's say mod now this needs to be accessible from outside so module exports uh and then the function name which will be get genres all right so um actually this should be equals function all right and this function is going to take in a call back which will will enter through the route file and then an optional limit okay okay and then inside here we're going to say genre uh genre. find okay just like we
28:37
would in the shell and then that's going to take in a call back and then the optional limit so we'll say do liit and then pass in whatever comes from the parameters and that's it all right so that should get genres 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 uh genres equals I'm sorry no we don't want to do
29:17
that we're going to say genre with a capital G um is going to equal require and this this is in the models folder so we'll do dot do slash models SL genre. JS actually I don't think we need the
29:47
JS 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. getet genres all right and then in here we're going to pass in that call back 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 customers
30:17
genres all right and then we can say if there is an error then let's just we'll just say throw error okay if there's not then we're going to say uh res. Json because that's the format we want to send and then we'll just pass in genres which comes from here all right so we'll save that and let's see what
30:48
happens okay nothing um oh did I no 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 just new to add it all right 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 CU it's structured the same way we're just going to have extra
31:33
Fields so book
31:39
schema okay we're going to have actually let's change all this
31:55
first okay and then up here let's see all right so book schema we're not going to have a name it's going to be a title type string required um and then we're going to need uh we're going to keep the create date so let's just add to this
32:20
genre see a
32:25
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
32:44
description and I guess we don't have to require that uh let's see
32:55
author publisher
33:01
let's make that not required um the
33:10
pages and this could be a string or a number uh let's let's just keep it a string and not required all right and then we're going to have an image URL
33:34
and a buy
33:39
URL and that's it all right so that's those are the properties now for the methods let's say get
33:52
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
34:08
this and we're going to change this to API slash books and then we're going to call book doget
34:23
books change this right here to books and then 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
34:43
that all right so let's see if that works if we go to API 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 U 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 you know
35:24
what let's change this to get book by ID all right and then the function is going to take in uh book and a call back we can get rid of limit because it's only returning one all right and then down here we can use book. 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
36:01
this all right and we're going to take that limit off and that should do it all right so now we'll go back to appjs Let's copy this all right now it's going to be API SL books slash and then colon ID all right so that's going to represent whatever the ID is that's passed in through 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 uh
36:46
request request. params do ID actually you know what let's do underscore ID here and here
37:01
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 all right 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 now it's giving us just that one book so now we can get genres we can get 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
37:41
model actually you know what let's do the genre first because that's a lot easier all right so we want to add genre all right so what we're going to do is uh let's copy this
38:02
and I'm going to change this to add genre 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
38:34
object all right and we want to get rid of this
38:42
limit let's save that and then we'll go to appjs and let's go up here after the get genres we'll copy that and we're going to change this now to app. poost because it's we're taking care of a post request and it's going to be the same URL all right 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 and a delete for the same URL as well all right so what we're going to do now is let's go right here we're going to say
39:26
VAR ja gen is going to be equal to request. body and this is where body parer 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 say genre dot add genre all right and then we're going to also pass in that object and then we have our call back change this to just genre and then same thing we're just going to uh respond with genre all right so let's save that now
40:13
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 uh a Chrome app for that I don't know if I have it in this particular machine I don't think I do see rest easy
40:48
Chrome all right so we'll add this to Chrome
40:59
okay so now let's open that and now we
41:06
should we should be able to let's see we want Local Host API SL 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 a 200 okay 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 uh let's see query parameters we have the headers okay so for headers we're going to add in the content type and that's going to be application SL
41:56
Json 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
42:16
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
42:34
out okay looks like it's it's hung up on something oh I know what it is it's the middle Weare for the body parer all right so we want to go back to appjs and go to the top and we're going to go right here and we're going to say app.use this is just the it's just one line of middleware just to kind of initialize the body parser all right so app.use body parser and we want we're using Json so body passor do Json all right so that should fix it so let's go back and try again actually we got a
43:27
I wasn't using node modon that time so I got to
43:34
restart and I might have to reload this whole page okay good there we go status 200 and it gives us gives us that row all right so let's go check it out so in the shell we'll do db. genres. find 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
44:21
this okay I'm going to pass in a book
44:33
save that okay now we'll go back to the appjs 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
45:02
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 um it's not it's not very safe to just have whatever someone adds while they make a request to go into your database all right um we do have Mongoose which uses models which makes it a little more secure but um you want to kind of dissect the fields and and insert them SE uh individually and you also want some kind of authentication so there's a lot of
45:37
security measures to take um if you're going to be work if you're building a production app okay this is going to be add book all right that looks good save that all right so let me restart this I'm going to switch to use
46:04
nodemon all right so now what we're going to do is let's change this to books and post okay so we got to add some things here let's change this to
46:23
title and let's go back to Barnes & Noble
46:36
all right let's see subjects we'll get a romance novel okay this looks good oh actually let's get the notebook all right so I'll grab this description
46:58
okay
47:05
title and then we're going to have the genre which will be
47:14
romance
47:20
description put that in
47:28
let's see what else do we need the
47:36
author Nicholas
47:48
Sparks okay publisher
47:56
say Fubar uh what else did we have
48:06
pages and I'm just going to say 400 and then we need the image URL whoops we need quotes image
48:24
URL let's see copy link address
48:34
all right and then we need the buy
48:46
URL all right I think that's everything let's check description publisher pages yeah that looks good all right so let's go ahead and try to send this to API
49:06
Books Okay so it gave us the object back now let's go verify so we want uh db. books. find. pretty and there it is The Notebook okay so now we can we can get data we can we can post data now we want to be able to update data all right so let's go to uh we'll start with genres so we'll go to the genre model all right so this is going to be a little different because we actually have to take apart the the um the object being submitted so let's change this to update
49:59
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 call back all right and what we want to do let's create a quick
50:30
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
50:52
name name will be genre. name so we actually have to assign each field um 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 all right and then we're going to say genre Dot and then mongus has a function called find one and
51:16
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
51:36
options all right so that's the model function now let's go back to appjs and let's get just copy the post actually we're in genre so we'll copy the genre's post and then we're 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 all right and then let's see we'll
52:14
say VAR ID equals request. params do let's change it to an underscore idid you don't have to but just because that's what it is it's in the database as all right so we got the ID genre is still going to get the body so we need that um 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
53:02
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 want to change this to be put and
53:46
then let's change this we'll say name and let's change it to to Thriller okay so that should work doesn't mean it's going to all right so I'm not sure if if it did work let's go check so we'll say DB dot genres do find and it did look name Thriller all right so for some reason when it gives us the thing back it's still suspense um 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
54:40
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. put and API books slid let's change that to book and then we'll say book. update book which we still going to create change
55:12
that okay we want to change this and
55:24
this okay we'll save that and then then we'll go to the book model and let's grab let's copy the update genre paste that in
55:42
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
55:57
okay we're going to need all the rest of them as
56:04
well so title and then we have genre whoops these should all be
56:21
book all right so we got title and then genre which is going to be equal to book genre
56:38
description what else do we got
56:45
author publisher
56:57
um let's see what else we need our URLs so we have the image URL
57:15
oops and then we have the buy URL
57:26
oh Pages
57:31
too I think that's
57:38
it all right then we're going to change this to book. find1 an update we're going to actually the rest of that's
57:50
okay all right so save that and then we'll go over here we need to get a 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 murderous take that ID and then we're going to go API 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 murderous
58:30
send and let's go check it
58:35
out so genres do no not genres
58:45
books all right so let's see title murder house okay now what happened here is everything else went null 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'd have the form pre-filled 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 overwrote the whole thing and you can um
59:27
use different types of queries like we could have used uh I think it's set and that would have just updated just this field but um 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 and let's just we'll copy this say delete okay we change this to
1:00:07
delete and we're just going to call genre. remove and that'll take actually we need to set the query so I'm going to copy what we did here in 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 uh app. Delete same URL uh we'll get the ID we don't need this because we're not getting we're not um posting any data so we can get rid of that and then we're going to say book Dot remove book is that what I called it oh
1:00:59
I'm sorry we're doing we're doing genre so this will be
1:01:11
genres okay and then that's going to be genre. remove
1:01:21
genre and it's just going to take an ID d and a call
1:01:30
back all right and then we're just going to return genre all right let me just check that model real quick delete genre okay all right so let's try that so we need uh we need an ID
1:01:56
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 SL genres slash the ID we're going to change this to delete get rid of this body and send okay so we got a 500 error genre. remove genre is not a function uh let's
1:02:33
see oh we have delete genre not remove all right let's change this to remove and we'll try it again ID is not defined oh we got to change this to ID
1:02:57
all right so it looks like it worked let's check it genres. 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
1:03:31
now this is something that you could use for um multiple programming languages all right so as long as you can make HTTP requests from whatever language or 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
1:04:14
one okay this will be remove book
1:04:27
all right save that let's go get an ID of a book okay let's
1:04:37
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 slid delete send and that should be gone let's go back to this page reload and it's gone all right so we now have whoops spelled 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 uh
1:05:27
intricate as you'd like as far as the structure of the Json documents you can use arrays you can use embedded data but this is a a good solid start to um to building a restful API so hopefully you guys enjoyed this hopefully you learned something from it um if you like the video please give a like subscribe whatever you can do and in the next video uh we'll be making a front end to interact with this API all right thanks guys
Here to help you learn
Sign in to keep a private conversation with your course tutor.
Ask me anything about the material…
Sign in to chat