Vanilla Node.js REST API | No Framework

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what's going on guys in this video we're going to create a vanilla node.js rest api and it's going to have full crud functionality we'll be able to create read update and delete from a json file and usually when you see tutorials on node apis you're using express or maybe some other framework usually express though that's definitely the most popular but in this case we're not going to use any of that we're just using bare bones node with the http module so it's going to be harder to write because we don't have you know the additional bells and whistles of a framework like express but i think it's a good learning experience we're going to get into the you know the http resp request response cycle how to structure our code we'll create a controller and a model to get our products um we'll have to you know figure out what route is being hit what method is being used we have to do all that manually so like i said it's going to be a bit harder but it's a good it's a good way to learn kind of what's going on under express under the hood all right so you do need node installed obviously if you don't have it go to nodejs.org and if you're brand new to node.js i would suggest watching my crash course before watching this and also i'll be using postman to make requests to my api so if you want to grab that you can get it from postman.com all right and i have postman open right here so we can just put our request in and whatever method we want to send all right so as far as what i have here it's just a folder called vanilla node rest api and i have a data folder with a products.json file so we have five different products in here and each product has an id a name a description and price and we're going to be able to read and write to this file you could of course use a database but we're just going to be dealing with the json file so we will be using the file system module that node comes with all right so let's start off here by just creating a package.json so npm and net we can add dash y to just skip the questions and that gives us a package.json and the only third party package that i want to use is nodemon so let's say npm install dash uppercase d for dev dependency and then nodemon and that'll just allow us to monitor our server.js files so that we don't have to keep restarting the server whenever we make changes now we do have to add a script i'm going to add two here one is going to be just a standard start script so if you deploy this is what you would run and that's just going to run node and then whatever we call the file which i'm going to call server.js and then we're also going to run node mon with the npm run dev command okay so we'll save that that's all we should have to do here close that up and then let's create our server.js which is basically the entry point for this and we want to bring in our http module so we want to require http this comes with node.js this isn't something you have to install or anything like that this is what express and all those other frameworks use under the hood now this http object has a method called create server and this this basically kickstarts everything we're able to pass a function in here that takes in a request and response now whenever we make a request to our server we have access to the request and response objects so we can get some data like we can get the method and the url and stuff from request we can respond with certain headers or you know whatever we want to put in the body with this res object now you have to call dot listen on this and then put in a port but what i'll usually do is separate this out and create a server variable and then down here we'll call server.listen and then it takes in a port number so i'm going to create a variable called port and i want to use 5000 but i also want to check to see if there's an environment variable so process.env.port or 5000 and then we'll pass in port and we can also pass in an arrow function and i'm just going to do a console log with some backticks and say server running on port and then dollar sign curly brace syntax and we can pass in the port variable all right so if i go ahead and run this with npm run dev nodemon is going to watch server.js and you can see server running on 5000. if i go over here and i make a request http localhost port 5000 and i send it's going to hang it's not doing anything so i'm going to cancel this and that's because we haven't done anything in this function if i do a console log here of one two three and i send a request it's still gonna hang because i haven't responded with anything but you'll see it is logging one two three down here and it doesn't matter what the url is as long as it's localhost 5000. all right now to just show you like a a generic response here let's just send an html page so first thing we need is a status code so we can set res status code to whatever we want i'm not going to go over all the status codes but basically 200 means everything is okay that's successful 201 means something is created and successful the 300 range is redirects 400 so 400 is a bad request 404 is obviously not found i'm sure most of you know that one and then the 500 range our server errors so we want to set this to 200 and then we want to set whatever content type we're sending back in this case it's going to be html so we want a res dot set header because the header is where you put stuff like the content type authorization tokens and stuff like that so we can set the header uh of content dash type and let's set it to text slash html and then we can send in the body so res dot write and in here we can actually put in html because i use that content type so the browser will render it we'll just say hello world and then we need to end the response so res dot end okay so let's save that and now if i'm actually going to test this in the browser because i just want to show you we can go to http localhost 5000 and it renders our html and if we look in our network tab and i'm just going to reload and click on localhost you can see the request url that it was a get request obviously because it's the browser status code 200. and if we look at the let's see i want to show you the content type which is request response headers where is oh it's right in front of my face so content type is text html all right now we can also get this in postman so we if we want to render the html we can hit preview but you can see we get 200 response if we look in the headers text html so that's kind of like the i guess this the simplest kind of response we can give um and it doesn't matter if we go to like slash whatever slash abc it's still going to give us that hello world because we we didn't specify any specific url or method or anything like that with express you have the luxury of doing like app.get whoops app.get or app.post or something like that put a specific url in and then you have a function well we don't we can't do that because we're not using express so we have to kind of manually do that now what i want to do here is instead of sending html i want to send our products okay so i'm going to bring in say const products equals require and that's in dot slash data slash products now we can actually shorten this up a bit if you want to send the code and this header you can actually use write you could use right head so res dot right head and you can pass in a status like 200 and then an object with your header values so let's do content dash dash type and set that this time i'm going to set it to application slash json because we're not sending html we're sending json now even though we're bringing in products.json it's still going to come in as just an array of javascript objects so when we send this back so we can say res dot write and instead of just passing in products by itself we have to wrap it in json.stringify and then pass in products now you don't have to do this with a framework like express it does it for you but in this case we do have to and we can also shorten this so instead of doing res dot write and then res dot end like that we can actually just do res dot end and pass in our response so we don't even need res dot right so we can just do that so that kind of shortens that shortens things up let's save that i'm going to go back here and send and now i'm getting all my my products now notice i'm making a get request to slash abc it doesn't matter what i put in here whether it's slash you know one two three or whatever i'm still going to get the same response because we didn't specify any conditionals on the url or even the method if i do a post request same thing no matter what type of request i make i'm going to get these products so oops so we need to figure out how to kind of you know test the the url and the method so in here let's do an if statement and this request object we can get the url which is going to give us everything past everything past the the the host here so if i do slash api slash products it's going to give us that slash api product so let's test and see if that's equal to slash api slash products and then let's take this here so we'll take that and move that up into this function here not function into this conditional and let's save that and now if i hit api products we get this if i do api posts or something else anything else it just hangs so let's take care of the hanging so we'll just do else and i'm going to grab this and we basically want to want to send back a not found alright so not found instead of 200 we're going to do 404 and i'm going to send back jason but it's just going to be a message so i'm just going to say message and we'll say route not found so now if i go back and i go to api posts i get route not found if i go to anything except api products i'm going to get that so api products good now we still have an issue here if i make a post request same thing put requests same thing we don't want that it should only be a get request because get is to do just that get data from the server so in addition to testing the url let's also say if the request.method because that will give us the method let's say if that is also or if that is equal to get in addition to this url then we want to go ahead and send the products so that works but if i do a post then we get route not found okay so this is stuff that you know express gives you some really handy tools to to to work with but in this case we have to do it manually and it's it's more of a pain in the ass but you know it's a it's a learning experience and i i like doing this kind of stuff anyway so now let's clean this up a little bit because we're going to have quite a few routes we're going to be able to it's going to be a crud api so i don't want to put everything inside these ifs i want to kind of separate it out a bit so we're going to have models and controllers so let's create a models folder in the root and create a product model.js and then i'm also going to create a whoops a controllers folder so controllers and let's create a file called product controller.js now if you're not familiar with the mvc method or mvc design pattern it's model view controller we're not dealing with views because this is an api you could use react or something for the view models deal with databases or i shouldn't say databases data in this case we're dealing with a json file so it's this is these functions will strictly be used to either get data or create or update or delete data controllers are going to basically uh you know control whatever that particular route is doing you know the status it's sending what it's sending back any headers we want to send stuff like that and it's also going to interact with the model it'll get the data from the model to send back okay i hope that makes sense so let's kind of let's change this up a little bit i'm going to take the products that we bring in and cut that and put that into the model we just need to add dot dot because we're going up out of the models folder so we'll bring that in and then let's create a function here called find so these are very just you know low level functions say find create whatever and for the models i'm going to use i'm going to return a promise all right because we're getting data so i mean we we don't technically have to use promises here but usually when you fetch data from either a database or whatever you'll return a promise so we're going to say return new promise and if you've never done this before promises take in a function with a resolve and reject okay so when the promise is fulfilled you can resolve and you can send back data in this case this is going to be super easy we're just going to resolve products okay so we're getting the products from our file here and we're just sending a pro returning a promise with those products now we do have to export so let's say module dot export since we're using this in another file and we're going to export find okay and then in our controller here that's where we want to do this stuff so i'm actually going to just cut that and in our controller let's first bring in our model so i'm just going to call it uppercase product just like you would with something like mongoose and then require dot dot slash model slash product model and then let's create a function it's going to be asynchronous because we're going to call our model function which returns a promise so async function i'll call this get products and these all these controller functions are going to take in a request response and then i'm going to put everything we do in inside of a try catch and if there's any kind of issue here then we're going to console.log the error all right but in here i'm going to paste in what i copied or what i cut from the other file from our server and we need to fetch our products because right now it doesn't know what this products is the file was brought into the model not the controller so here we'll say const products and we want to use a weight here because remember that find function returns a promise so we can say from our product model we want to call dot find what i call it let's call it find all actually so find all that and then we could say product dot find all and this is going to be the same so 200 whatever sending back products which now is going to come from this function which ultimately comes from the file so to use this controller let's module dot exports get products and then in our server js i'm going to bring in we could say like product controller and then do product controller dot get products but i'm going to just use destructuring and pull out get products from that from the controller and then down here we can simply call uh get products we just want to pass in request in response which comes from here okay because if we don't pass that in then in our controller we it's not going to know what this is and what this is so now let's go back to postman and let's send okay so we got an issue here let's see get products is not a function so get products from our controller and let's go to our controller oh this should be module dot exports all right let's try that again okay so we're getting the same you know the same result that we got before but our little our api here is structured much better it still shouldn't work if we do like post or anything like that okay so now that we have our controller and and model files created we can easily add functions so let's create our next route which i want to be i want i want to be able to get a specific product by its id now again with express you could put you could do like slash api slash products slash colon id and you could access that don't type this i'm just showing you you could access that with request.params.id which makes it really easy this would give you whatever is passed in here we don't we can't do that here so what we'll do is create another let's say else if just move this up here so else if and instead of saying if the request.url is equal to something i'm going to use dot match and we're going to match a regular expression basically we just want to see if it's if it matches api slash product slash and then a number then we want to go ahead and fire off you know whatever we're going to do here so since we're passing in a regular expression we need to add in our forward slashes and i want it to be slash api products just like this so if i take this and i put it in here any slashes we have to escape with a backslash i want to put a backslash here and here and here and then after this should be another another slash here and after that we want to open up some parentheses with some brackets and say zero through nine plus so it can be you know product slash one or it can be product slash one thousand all right now we also wanna make sure the method so let's say right here and the request dot method is equal to get if that happens then we're going to have a controller called get product or controller function called get product that will take in request response and id now how do we get this id because remember we can't do request.params.id what i'll do is create a variable called id and i'm going to set this to the request.url which is going to be api products and then some number and what i'll do is call split which takes a string and splits it into an array and i want to split it by the slash so that way it'll be like this api slash products slash and id it turns it into an array where this is the zero index this is the two this is the three index so i want to get the three that's going to give me the id all right and then we'll just pass it in to get product which we're going to pull from the controller so right here i know we haven't created it yet but we will so we'll pass in get product so let's go i guess we'll yeah we'll just go into our controller now and let's actually i'm going to put a comment here and just put the description of this so this gets all products and then the specific route for this is a get request to api products okay and then i'm going to copy this and this one is going to be get let's say get single product it's going to look like this the route get product it takes request response but it also takes in the id from the url and here we're going to say const product singular and then we'll have a method or a function in the model called find by id pass in the id and we just want to check to see if the product exists so if the product doesn't exist and then if it does so i'm going to grab this and put that in here so if it doesn't exist i'm going to pass in here 400 which is a bad request and then we'll send jason message and say product not found okay actually let's this will be a 404 not a 400. and then else else then we're going to go ahead and this is fine we just want to make sure we get rid of that s because we're sending back a single product all right and then we need this find by id so let's go to our model actually let's make sure we export that before i forget so get product and then in the model here let's copy this and let's say find by id and that takes in an id and we're going to return a promise but we need to get a specific product so what i'll do is set product equal to products which is our array of products from the file and let's do dot find so find we can pass in an arrow function i'm just going to pass in p so for each product i want to get the one where the product id is equal to the id that's passed in and then i want to resolve my promise with that product okay and then we just want to export down here find product by id or i'm sorry find by id all right and you could make this into an abstraction you know where you know it actually doesn't have to be products it could be whatever but uh we're just dealing with products so i don't want to make this too complicated so i think we should be good we're bringing in our controller function get product we're passing in request response id i'm just gonna move the controller here and then in the controller we're finding by d with this and checking to see if it's that not there if it's not we'll send 404 if it is we'll send the product good all right so let's try it out so this should still work and then if i go to slash one and send you can see i get a 200 response and i get my single product if i go to two i get that product if i go to 22 i know there's not a 22 i get product not found okay cool so now we're going to get into kind of the more difficult stuff where we're actually adding data posting data to the server so for that let's see how i want to handle this so let's first of all put the if statement in the route here so we have that so let's put an elsif here and you could definitely improve the router here you could create a router that is much neater than this but let's say else if the request dot url so this is this is going to be a post request to slash api slash products so we don't need to do like the match or anything we can see if it's equal to that you could do a match as well but we want to just check to see if it matches that and if the request dot method is equal to post this is a post request okay and if it is then we'll have a function called create product just pass in our request response okay and then up here let's bring in from controllers create product and let's go into our controller and create that i'm just gonna copy actually i'll just copy this one okay so this is going to create a product it's going to be a post request create product okay so this is uh let's see this is probably going to be our one of our longest ones here so i'm going to get rid of everything in here in the try and before we actually get body data because we want to get data from the body um you know sent through through json but before we do that i just want to show you how we can actually insert something static for now so i'm going to just create a variable called product set that to an object and that will have a title let's just say test product and a description this is my product and a price just say 100. all right so we have this product i'm not including the id because i want that to get automatically inserted like it would in a database so let's see we have that product and then what we'll do is we'll have a model function called create so let's say const new product equals our product model dot create and then we pass in uh we want to pass in the product and then once we do that we'll go ahead and return dot end and jason dot stringify and i wanna i wanna return the new product like that actually we want to send the status as well so let's do status and the header the content type so let's write head and we'll do a 201 which means created and then content type application slash json okay now in our model here we want to have a create now as far as creating the id there's many ways we could generate an id i'm actually going to use a separate uh package i actually forgot about this when i told you nodemon was the only one i'm using but it's just a package called uuid so let's say npm install uuid just to generate an id for us and then let's run our server again and since we're we're constructing this in the model i'm going to bring it in here so let's say yeah we'll do const so the way we can bring in the uuid is we do v4 and this is in the documentation if you want to check it out uuid version 4. there's different versions and we want to set that to require and then uuid so now let's create a create function so i'll just copy this okay so this will be create and we don't need to pass anything in turn a promise and let's just get rid of this whoops get rid of that so we it actually i said this is going to take isn't going to take anything it's going to take in the product right because in our controller we call product.create we're passing in this product which right now is just a static object but we're going to pass that in but we need to add an id to that so let's say const new product set that to an object and we want to spread across all the stuff that's in the product that's passed in so we're just using the spread operator which will take all the key value fields in this object and spread them across this object and then we want to just add an id and get that from uuid v4 that's going to generate random id or a unique id okay so it'll have the id and then whatever is passed in the title description and price here so that's going to put that into the variable now we want to take products which is from our file and that includes all the past products we want to push on to that the new product right so that'll push on to that now we need to add that to the json file okay because i want to actually update the file so what i'm going to do is create a utility function to write data to to a file so in the root here let's create a utils folder and inside utils actually not a folder let's do just a file so we'll create a file in the root called utils.js all right and in this utils file i'm going to bring in the fs module which is to deal with the file system fs and let's create a function we'll call this write data to file and it'll take in a file name and it'll take in content and on fs we have a method called write file sync so we're going to just make this easy on ourselves and write this synchronously pass in the file name and then the content although we want to stringify the json content so json.stringify and pass in content and then we can also pass in let's put a comma here we could pass in the encoding which is going to be utf-8 and then we pass in a function with a possible error and we'll just check for that error so if there's an error then we'll go ahead and console.log error okay and that's it so we just want to export this so dot module.exports and we want to export right data to file and then we're going to bring that into our model because that's where we want to use it so in the model let's go up to the top here and let's say const write what i call it write file to data what i what did i call it write data to file all right write data to file and that's going to come in from our utils which is dot dot slash utils all right so now back down to the create so we have products with our new product push onto it so now let's call write data to file which takes in the file name which is dot slash data slash products.json and then the content which is going to be products and then we just want to resolve the new product because that's what we want to send back when we call that in our api we want the the new product sent back to us so pass down here create okay so in our controller so that's going to give us the new product back it's going to get placed here and then we're going to respond with it so let's go ahead and give that a shot now remember it's it's not going to have body data yet i'm not dealing with that just yet it's just going to be this test product so we don't even have to send any body data yet so i'm just going to make a say just products and a post request and send okay so something went wrong here create product is not a function i must have forgot to bring it no product oh wait a minute um function create product yeah i didn't export it down here create product let me just make sure i brought it into server okay i did all right so let's try that again what's this comma okay let's try it again make sure it's a post request send okay so we got a 201 created but we didn't get our new product back so let's check out the json file so it did get added as you can see we have id5 that was the last one that was there initially then we have this id which is a uuid title test product all right so that worked it's not all nice and neat anymore but it's fine if we if we make a get request two products we should see it down at the bottom here okay there's our test product i just don't know why it wasn't sent back to us so let's see kant's new product pass it in here and then we resolve the new product and then in our controller i didn't wait returns a promise so we have to make sure we await now you can if you want to manually go into your products.json and just get rid of the last one here so you don't have a bunch of these test ones just make sure you don't delete the bracket and then we can try this again so we'll make a post request to api products and there we go so now it's getting inserted and it's giving it giving it us back when we make that request so now we want to work on body data right we want to be able to make a post request and send you know raw jason and then send here you know a title say brad's product a description and a price okay so we want to be able to send that and get that data so the way that this works is a little confusing and the syntax is is not that great but we're again we're not using a framework so we can't just do request dot body dot title like we can with express so i guess doing it this way really shows you how to appreciate express and appreciate other frameworks but what we'll do is we first want to initialize a variable called body and this is just like straight out of the documentation so i want to initialize that and then this request object we want to call dot on and we want to say dot on data and then we have an arrow function now this arrow function takes in a what's called a chunk and what we end up with here is a buffer and we need to convert this buffer to a string by saying body which is just an empty string right now and then we want to append on to that plus equals chunk dot and then to string okay so we're just taking that buffer and converting it to a string now we also want to go after that request dot on and do another request dot on and here we're saying on end so request dot on end then we want to we'll have a function here and then we have access to body right we'll have access to body which will be you know body dot title or whatever actually we'll have to parse it to do that because it's going to be just a string you know a json string so what we can do is take uh let's see so we'll take this because we can't we can't keep it down here we have to go within this dot on end and we'll paste that in there and like i said we'll have access to body but i want to parse it so json.parse body and let's we can take out from this let's say const because this is now a j uh a javascript object we can destructure and we can take out values i want to take the title description and price okay make sure this is in this request dot on end and then let's see our product is being let's actually take this and put this in here as well except we don't want you know hard-coded values in here i'm just going to do title description and price because these these are all the same as these so we don't have to do you know title title so it's going to take what we get from the body here the parsed body put it into this object and then we're going to pass that into the model create it's going to create a new product and then we're going to respond with that new product so let's see looks like we have an issue here okay so a weight is only valid in an async function so now even though this is async this await right here is actually inside of this function so we have to label this async okay that's why we're getting that message now we should be good so now let's go ahead and keep this in the body this title description and price and i'm going to send and i get back an id random id brad's product so that was now inserted into the data into the json file we can check the json file here and you can see that it's been added all right cool so i'm actually going to get rid of it though and the test product i just want the initial data that we had but that's how we can get data from the body now we can make this easier on ourselves because when we do like an update we also have to do all this right to actually get data from the body to update so let's let's create a utility for this so that we don't have to keep doing that so i'm going to go right under the write data to file and let's create a function called get post data and that's going to take in the request we need to pass that in because we need to call you know request.on data and end and we'll make this a promise so return new promise and that takes in resolve reject and i'm going to pass this into a try catch as well and then here let's say let body equals nothing and then request dot on let's say request dot on data then we have our chunk okay and then we want to convert that to a string so body is going we're going to append on to that plus equals chunk dot to string and then we have our request dot end not end dot on end and that's going to take a function and then we want to resolve so we can call resolve here the body all right and then if there's an error we'll go ahead and reject and send that error all right so that should do it let's go ahead and export this so get post data and now if we go back to our controller let's bring in that get post data so dot dot dot slash utils right where are we controllers yeah so we want to bring in get post data and then to use this let's go down here and it's not going to be as confusing because we had to move everything into this end but since we use the promise and we just resolve the body we should be able to do this a little a little better so what i'm going to do is just grab this so the product this right here we call create and then the response i'm going to cut that did i cut it i'm going to cut that and then i'm going to get rid of everything else that's in this try okay so we have this a clean slate here and then we can create a variable called body and we should be able to await get post data and we need to pass in request right because we pass in request here and this returns a promise and when it resolves it resolves the the body data right so that's what we're going to get right here so we're going to get from this and we're going to put it in this variable then i'll just paste this in and actually one thing i need to do is get the data from the body so again i'm just going to say title description price from json.parse body and then this all should work just the same so let's save that let's give it a shot this looks much cleaner now so let's go over here and i'll just say brad's product 2 and send i get back brad's product 2. if i make a get request i'm just going to open up a new tab here make a get request to products and at the bottom here i should see brad's product too okay so we can get all all products we can get a single product we can create a product all we have left is update and delete so let's go back to our server.js and now we want to check for a put request to products and then the id so i'm actually going to copy this elsif right here because this this is the get you know where we match to the id and i'm going to go under go right here paste that in and so else if this right no yeah this is looking a little weird all right so else if this now that's fine that's going to check to see if it's api products id but i want to check to see if it's a put okay and if it's a put then i want to update but i do need the id so i'm going to do the same thing i did here and get the specific id okay i'm just splitting the you know the api products and the id and i'm getting the last putting it into an array split by the slash and getting the last value which will be the id and then i want to call update product and pass in request response okay now we haven't created this update product yet so we'll do that but let's just bring it in here before i forget and go to our post controller and we're gonna copy the create because it's pretty similar the way that we use the you know the get post data so i'm just going to copy that whole thing paste that in and say update a product which is going to be a put request to slash id and let's change this to update product and it's also going to take in an id and see so we want to first find the product so let's say const product equals and we're going to await our product model and we already have find by d because we want to make sure it exists and then let's say if the product doesn't exist then let's actually i'll just copy it from up here so let's see right here where it doesn't exist i'm going to just grab that and put that in here and then we'll have an else else then i'm going to just copy the rest of this that's in the try cut that and put that in the else and let's see so just move this over so if the product exists then we're going to get the body post data we're going to pull the title description and price out and then here i'm going to set the title to either the title from the body if it's there or just whatever was there so product.title that way we don't have to send every field we can just send just the title if we want or just the description or whatever so oops so description or the product dot description price is going to be either the price from the body or the product dot price okay and then down here let's call this upd product for updated product and set that to a weight product update and we want to pass in the id and the new product from the body actually you know what let's not call this pro we can't call this product let's call this product data and then here pass in product data okay and then down here what we want to respond with is a 200 because the 201 is created we're updating and we want to respond with that upd product all right now we need to create the update model function so let's go into product model and then from here let's copy the create and paste that in it's going to be updates and it's going to take in in addition to product the id because the product right here is just going to be the title description and price so the id will get passed in separately and then here we're going to say um let's see let's actually let's get rid of this because we need to update one that's already there and then rewrite the files so let's get the index of the product we need so we can take our products in the json file which are you know they come into this file as a javascript array and we're going to use find index so we can find the correct index of the product we want to update this takes in an arrow function let's say p and we want the index where the product id is equal to the id that's passed in okay and then once we get that index we'll take the products and that specific index we want to set to have the whatever's passed in here this is an object with the title description price we want to spread that across here we also want to pass in the id all right so we have the id the product then we want to write to the file so here we're going to pass in you know write data to file pass in products and then resolve i'm going to resolve the single updated product which we can get by doing products and then that specific index all right so we'll save that actually we have to export update let's make sure to export update here and in our controller we need to export update update product and then in our server.js we're calling it here bringing it in good so let's give it a shot so we have let's see let's do uh we'll do this this one here brad's product two i'll just grab the id and throw that in here slash that and then make a put request and then in the body let's say body raw jason and i just want to change the title i'll change it to say title and i'll change that to updated title and send and let's see we get back so we get back the same product but you can see the title has been updated okay so i don't even have to send description and price if i just want to update that and if we check the json file just to double check it right here you can see title is updated title awesome so the last thing we have to do is the delete which isn't that difficult so let's go down here and i'm gonna once again grab this elsif because we need to get the id all right so here we're saying if it matches api products and then an id but it's a delete then we're going to get the id and we're going to call a controller called let's call it uh remove product okay and that's going to get passed in request response like every other controller function as well as the id let's bring it in up here actually we'll call it delete product the model function we'll call remove because you can't use delete as a function name so delete product let's go into our controller and let's copy let's copy the get single this right here so get product copy that go down to the bottom paste that in so this is going to delete product it's going to be a delete request to product id and this is our controller function which is called delete product takes in the id we're gonna look for the product first if it's not found we'll send back this product not found if it is found then let's call a weight we don't need to actually capture anything from our model function but we do need to call away because it's going to be asynchronous product dot remove and then pass in the id all right and then as far as what we want to pass as a response let's put some json in here and just put a message and we'll put some backticks and we'll say product id has been or we'll just say product id removed all right and we need to export delete product and then finally we have to create this remove model method so here let's see what do we want to copy it doesn't really matter i'll just grab this okay so this is going to be remove and it's only going to take the id not a product and then we want to get we want to filter out the product with that id and then just update the file so i'm going to get rid of these lines and say products equals now since i'm redefining products i need to actually change this up here to let because it won't let me if it's cons and i'm going to just set products directly to products.filter so we can filter out certain products here and i'm going to say for each product and then p so the current product dot id if it's not equal to the id that's passed in so that'll basically filter out whatever one whatever id is passed in here that we want to remove okay and then we're going to write to the file and just pass in the new products array and then what we want to resolve here is just nothing all right and then we just want to export remove and we should be good so let's try it out oops let's go here and i'm going to use the same id so if i make a get request to this so we can actually close that up we get this this object right here with updated title i'm going to try to delete this now so if i say delete with this id and send i get product and then the id removed if i go back to get and i try to get it it's not found because i just deleted it if i go to get all products that shouldn't be here so you can see that it's been deleted all right so we have a full rest api now we can create read update and delete we can read all products we can read a specific product by its id so i mean it is definitely more work than it would be with express but hopefully you know you learned a little bit here at the very least and that's it thanks for watching guys i appreciate it and i'll see you in the next video
Info
Channel: Traversy Media
Views: 103,633
Rating: undefined out of 5
Keywords: node, node.js, node.js rest api, rest api, node rest api no framework, restful api
Id: _1xa8Bsho6A
Channel Id: undefined
Length: 61min 37sec (3697 seconds)
Published: Thu Oct 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.