Deno Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey what's going on guys welcome to my den Oh crash course so I'm sure that your YouTube feed is flooded with dental videos right now it's definitely the next new thing however be cautious about people saying that no js' is dead and it's completely replacing it right now and I'll get to that in a second but basically in this crash course we're gonna do a few things we're gonna talk about what denno is and its features and benefits we'll have a few slides and then we'll jump in and write some code we'll start to look at some concepts like how to run files use parts of the standard library and namespace helpers use third-party packages we'll create a simple server and then we'll move on to create a complete rest api using demo and oak which is a minimalistic middleware framework ok similar to express or co-op so let's talk about what demo actually is it's a runtime for JavaScript and typescript based on the v8 JavaScript engine and the RUS programming language it was created by Ryan Dahl who was also the creator of nodejs which is also a JavaScript runtime that uses the v8 engine so if you're looking into Dino at this time I'm guessing you already know what nodejs is and it's most likely that a lot of you are already using it so you might ask why did Ryan dial create another JavaScript runtime well he announced it about two years ago and said that there was some things with node that he wasn't happy with and I think as developers we can all look back on some of our older code and see things that we could have done better and we could improve so I think that's what happened with Ryan and note although he couldn't just change nodejs at its core he had to create a new project and that's where demo came into play now there's some things that I just want to address really quick and I might rant a little here keep in mind this is my opinion nobody can really tell what the future holds but this is a very strong opinion backed up by facts so you might see crap like this no js' is dead etc unfortunately that's just how the Internet is bloggers and content creators they want to maximize views and they in tend to make stupid claims like this not all but there but a group of them out there denno is a brand new technology that was just released 1.0 was just released the other day and it is a good idea to start playing around with it which is why I'm making this crash course I think that in the future it's gonna be big and I think that it's gonna be a great alternative to no js' however nodejs is not dead it's not dying and it's not going anywhere anytime soon it has way too large of an ecosystem and it's way to establish so if you're if right now you're in the process of learning nodejs keep learning it do not stop just because denna was released okay and that's all I'm gonna say about that so let's look at some of the really cool features of demos so one we can use typescript and the typescript compiler is baked right in so all you have to do is create a file with a dot TS extension and you can then use typescript in your code denno is also secure by default it actually runs in a sandbox and in order to do things like read files write to files allow network access or access your environment variables you actually need to explicitly add these flags when running your application okay so if you try to do one of these things without adding a flag you're gonna get a permissions error and I'll give you some examples of that in a little bit so denill also offers decentralized packages one huge complaint probably the biggest complaint of nodejs was the way that dependencies were handled with NPM packages if he wanted to use let's say Express or some other package you would install it with NPM that would go in your node modules folder and it wouldn't be just expressed as a dependency but it would have all the dependencies of Express and then the dependencies of those dependencies so you end up with you know hundreds to thousands of folders in your node modules folder and denno has a different approach by using decentralized package management so there's no package Jason or node modules folder if you want to use a third-party package you can actually import it right from the URL at denno dot LAN slash X slash and then whatever package you want to use so here is an example of using the oak framework which we're actually gonna use in a little bit so we're importing application from and then the dental land URL slash oak and then the mod TS file and what happens is as soon as you import this it's cache to your hard drive onload so it's a little different it might take a little getting used to but at least you don't have this node modules folder with hundreds or thousands of folders inside of it so denno also has a set of standard library modules that are audited by the core team and guaranteed to work with denno so some stuff you'll find is like file system date/time stuff HTTP logs a lot of the stuff that you would find with the core nodejs modules and you would bring them in like this so this is an example of bringing in served from the HTTP standard library module and we're actually going to do this as well we'll create a simple server using this alright next let's see so we went through some of these features also modern JavaScript so node was created back and I believe 2009 and there's been a lot of lot of updates and improvements to the language since then so demo takes advantage of more modern JavaScript also we can use ES modules or we actually have to use ES modules which is the import syntax over the the common j/s require syntax that nodejs uses this is another really cool feature the top-level or first class await normally with a sync await you have to wrap your waits inside of a synchronous function you have to label it async but with demo we actually can use await in the global scope without having to wrap it in an async function which is really cool there's also testing built-in testing with denno there's a browser compatible API so you can use features like fetch there's a main window object stuff like that you can also execute wasm binaries so web assembly binaries you can execute those directly as well so these are just some of the really cool features that denno includes so as far as installation goes there's a few ways to install it you can use curl if you're on Windows you can use PowerShell or some of the other package managers like scoop or chocolaty I'm on a Mac so I used homebrew so I just simply ran brew install demo and it's installed and I'm able to access the demo command so that's what I would recommend if you're on Mac you can also use cargo as well if you're on Mac Windows or Linux ok so I don't want to do too many slides I know that some people aren't huge fans of them but I did want to just go over what demo is and some of its features and now we'll go in and give you some examples of that ok so first of all this is the denna web site Dino dot land and you can find you know the ways to install and get started and all the documentation is here so you definitely want to use this as a supplement to this crash course and any any Content that you watch with demo now what I'm gonna do is just open up not be s code just yet I'm gonna open up my terminal and I'm gonna run denno now I already have it installed I installed it with homebrew but there's there's multiple ways to install it which I just showed you on the home page but we'll just run demo and what happens is it actually runs denno repple so it opens up the repple which stands for read eval print loop and we can type in JavaScript here we can do you know one plus one will return to we can create a variable and we can take that variable and add on 200 to it it returns 300 so we can just type JavaScript in here very similar to the node repple but we're gonna get out of this with command C and then I'm gonna type in here denno - - help which will show us any options and sub commands so help log level could display the version down here we have some sub commands so repple is what we just actually saw denill run is how we can run scripts whether it's a local file or a URL which I'm going to show you in a second you can also install scripts as an executable I'll show you that as well we can we can upgrade NO we can run tests we can check the documentation for a module we can bundle modules and dependencies into a single file we can cache dependencies so a bunch of options here some environment variables like the base directory which by default is going to be your home directory and then dot denno so that's the help command or help option now I want to show you something real quick if we go to the standard library this is where you can find all the standard library modules so HTTP vlog FS I actually want to go to examples and there's some example scripts here one of which is welcome dot t-- s which you can see the code for it is just a console log now I can actually take this URL and copy it and go into keep opening that into my terminal and simply say demo run and put in that URL and it will actually run the script so we can see welcome to demo and what's nice is that we can run this the URL here and it will execute the file and we can go to that same URL on the browser and it shows us the school web page so it knows if we're actually running the script with denno or we're just going to it in a web page and we can actually run curl as well so if I say curl and then that script right there it'll show me the actual code it won't execute it'll just show me the code it won't show me all the HTML so it knows where it's being called from so let's try that with something else so if we go to colors dot TS you can see the code here but I'm gonna grab the URL and say demo run and paste that in and we just get hello world with some background colors and text colors ok now we can also install scripts so I want to show you if we go back to the standard library and we go to HTTP let's see so we're gonna be using this server ts in a little bit but I want to show you the file server not so much the code the code doesn't really matter to us but we can take this URL here and let's go back to our terminal and let's run demo run and let's paste in that URL now for this we're actually going to get a permissions denied error because remember it demo runs in a sandbox and we need to add these flags if we want certain missions so this actually requires read permission file read permission and network permissions so we have to run it with some flags so after denno run let's say - - allow - read and also - - allow - net so that will allow network access and read access so now I'll go ahead and run that and you can see that now I have a server listening on localhost for 4 507 so if I can even go into a browser and say oops it's alright localhost port 4 507 and it'll show me the files that I have in that in that folder which is just a simple test txt file now we can also install this we just ran it but we can install it with denno install so I'm gonna go back to my terminal ctrl C to get out of that I'm gonna run almost the same command except I'm gonna do install instead of run along with the flags of security flags so what's going to happen is it's gonna download it's gonna put it in my debt my base directory what's this all I already have it you know what let me just let me delete it real quick all right so I'm just gonna run that again and what happens is it stored it in my base directory which is right here and then inside a folder called bin and then file server which if I bring this over or you can see this is my home directory I have a folder called dot demo a hidden folder file called folder called bin and then this file server so now I should be able to just run file underscore server and it will do the same thing it'll start that local server up if I go here and reload to get the same thing okay so you can install scripts like that with denno install which is really cool and you might have to add it to your path you'll probably see a message here with a command to add it to your path all right so that's denno Ron and denno install though we haven't created any local files yet so let's jump into vias code now and let's create a file called hello dot TS and remember this can be TS or j s what I do is just use TS all the time and even if I'm not using typescript because you can always add it later but it's up to you so let's just do a variable here called greeting and we'll set that to hello world and then we'll do a console log of greeting and then I can run this down here with denno run and then hello TS and we're gonna see this compile because if there's any typescript it's going to compile it and then spit out hello world now if I want to add typescript I can do that it's a string run that same thing now if I set this to be a string but I actually set it to a number you can see I'm already getting this here but if I try and run this I'm gonna get an error right down here on the console ok because the typescript is compiled right through demo which is pretty cool alright so I'm just gonna put that back now excuse me I want to show you how we can use something from the standard library so I'm gonna create another file here called date/time dot TS and remember we want to import import from and then what I'll do is go back to standard library here and under date/time you'll see this mod TS so this is the date/time module and I can actually just grab the URL from here and then we want that in quotes and I'm gonna import two things one is called day of year and then I want current day of year now you're probably gonna get a squiggly line under the URL first thing you want to do is install the denno extension for vs code which you can see I already have installed so it's this one here by just J vac so you want to install that and you'll still get a squiggly line if it hasn't been cached so if I've already used this so it's already cached on my system so I'm not seeing that line but I'm gonna go ahead and use the day of year console.log that and then this takes in a date so we'll say new date let's to say 2020 - OH - OH - so it'll just give us whatever the day is of that and then let's do another console log with the current day of year which will just give me whatever today is the day of of today's date so I'll go down here and I'll run demo run date time ts I get 32 for this and I get 140 for this okay so that's just an example of how you can use standard ma a standard library module now we also have the denno namespace where we have different methods and things we can use so I'm gonna close both of those up and create a new file here let's call this create file dot TS and I just want this script to create a file called greet dot txt so first thing I'll do is create an encoder so we can use utf-8 encoding so we can say new you want to do text encoder and then let's create a variable we'll call this greet text and we'll set this to use encode our dot encode and and here we'll just put in hello world and we can put in a new line character and then I'll say my name is Brad okay so that's my text now I want to use the right file method on the on the denno namespace so denno dot write file now this actually returns a promise so I'm going to use a weight here notice I don't even have this wrapped in a function because we have that top level of weight okay so I don't have to even have it in an asynchronous function now if you don't have the denno vs code extension it's going to complain about this because it's going to expect it to be in an async function so here let's pass in a file name we'll call it greet dot txt and then the content is going to be the greet text like that okay so now we'll go ahead and we'll run denno run and then create file and notice I'm getting an error here because I need to add the allow write flag because we're writing to a file which might be a pain in the ass at first until you get used to it but I mean it makes it more secure overall so it is worth it and there we go so now I have a greet dot txt file hello world with a new line and then my name is Brad all right let's say we wanted to to read that file so we'll create another and these are just I mean this is just to test things out I'm not gonna have this code anywhere or anything it's it's just it's so little amount of code so let's say let file and we'll you will use denno dot open now again this is gonna be a promise so I'm just gonna put a top-level await on it and here let's read greet dot txt and what we want to do is I'll put this to standard output so we can use copy we'll say a wait demo dot copy and pass in that file and then we want it to copy - denno dot standard out so it's going to be STD and that we just want to take the file and close it so what you do is look at greet dot text and then it should I'll put it down here so let's say demo run and now we're going to need - - allow - read and then read file no such file whoops I put T ext that's not right let's try that again and there we go hello world my name is Brad now the last thing I want to do before we start our REST API is just create a very simple server so I'm gonna call this simple server dot TS and I'm sure that you've seen this example over and over and over in other videos so what I'm going to do is just go to the docs here go to the standard library HTTP and right here so I'm sure you've seen this block of code in articles and videos and all that so I'm not going to type it out I'm just going to grab it and paste it in so we're using serve from the server dot ES file here and then we're creating we're going to run it on a port so we pass in to serve an object with whatever port we want we can console log you know server running or whatever and then here we actually use this for weight syntax to get our request object and then we can add a response so here we're just awaiting calling this request of S which is our server and then request start respond and then we can add a body value which right here is just hello world okay so I'm gonna save this and now we should be able to do demo run simple server it's gonna give me an error because we need to add the allow net flag so let's run it with that so denno run - - allow - net and now it should be running on localhost 8000 if I open this up we see hello world so just a very basic server whenever you create something of any substance you're probably gonna use something like oak with no js' you would use Express you know not too many people just use the HTTP module and that's it you can't do much in it or it's just very low-level so what we're gonna do now is just get out of this and now I'm going to show you how we can build an actual API I just wanted to show you some of the basics before we got into that so now we're gonna go ahead and start with a brand new folder here I just call the denno REST API I'm going to open that up with vias code and we're gonna start to build our API and I'm also using postman to make our request so you can get it from get postman comm I believe so in my route here I'm going to create a server dot TS file this is going to be our entry point and you're gonna see a lot of similarities with node as far as how we set up our router and initialize our app and so on so first thing I'm going to do is import from oak application which is used to initialize our app and then router so we want to say from and it's gonna be HTTP den o dot land slash X slash oak slash mod dot TS and you'll probably have a red squiggly line that just means that it hasn't been cached on your system yet but once you run the application that should be fixed so now we can go ahead and initialize our app with application I'm sorry new new application and then we can call app dot listen and we can pass in here a port say 5,000 however this needs to have an a weight in front of it and remember we have that top-level await so we don't have to actually wrap this in an async function or anything and then I'm also I'm actually gonna put the port in a variable up here so let's set 5,000 and then down here we can just get rid of this since the key is the same as the variable and then we'll put a console log here as well and just put in some back text and say server running on port and just put that variable in there all right so if I save this and I try to run it let's open up I'm just going to use my integrated terminal and run denno run now since we need network access we need to have a loud - net and then we can run our server dot TS and it's going to give us an error about middleware so it says there is no middleware to process requests now with node and Express this would still work it would still at least run but we don't have any routes or anything so it's not it gives us this error so let's go ahead and initialize our router so router equals new router and then for the middleware we want to do app dot use which should look familiar if you're familiar with node and then router dot routes so we need that and then also allowed methods and we're gonna allow any methods you can restrict them if you want if you don't want delete or put or whatever you can do that but we're gonna allow all methods and then let's create our first route by taking our router we'll say for a get request to the URL of API slash version one slash and then the resource which is products and you don't have to have this here but I just don't know I like to format my api's like that so this will have take an arrow function as a second parameter now in here even it's going to take an object which you might see like context or something like that but I'm going to D structure because all I want is the response now since I'm using typescript I'm gonna say this is gonna be a type of an object with a response that can be anything and then in here what we want to respond with so we want to add to the body let's just say hello world all right so this should run so I'm going to go down here and run denno run allow net server TS so server running on five thousand so let's check it out will do make a get request to HTTP localhost five thousand slash API version 1 slash products and we get hello world and we get a 200 ok response by default okay so that's I mean that's the the fundamentals of how to just create a route which is pretty simple and pretty similar to Express now I don't want to have all my routes in this file here so I'm gonna create a file called routes TS and I'm going to just copy this top line here and then we can get rid of application because all we need to bring in is the router and then we can actually get rid of it from here and then I'll copy this line or cut this line where we initialize our router and also the route itself take that out put that in here and then we just want to make sure we export the default router ok and then we'll bring that in here so let's say import router no no curly braces because it was exported by default and that's gonna be from dot slash routes dot TS alright so we'll save that make sure this is saved and we should let's restart this and we should to get the same response so if we send this we still get hello world good ok so I also want to have a separate file for our functions I don't want to just put everything right in here so I'm gonna create a new folder called controllers and let's create a file here called products dot TS now in terms of data like I said I'm not gonna do anything to do with a database now I might do that or I will do that in a future video but for now we're just gonna put some hard-coded products in here so we have three products with an ID which is a string a name a description and price now since we're using typescript I'm going to create an interface called product and define the fields which will be an ID which is a string a name which is a string description and then we have the price which is going to be a number okay so that's the interface and we can apply this to our array here by setting it to product what it is an array so we just want to add in our brackets here now this we can actually move to a separate file so I'll cut that and in the route let's create a types dot ts file and just export that interface and any other interfaces you have in your application you would put in here and then we can just simply import product from and then we want to go up one level two types ts alright so that's our data now let's start to create our functions and the first one is going to be to to get all products and I usually like to put like some metadata here like the description let's do this description is going to be to get all products spaced that out a little bit and then let's also do the route so the route is gonna be get requests and it's gonna be API version 1 and products ok so now we'll create a variable well yes a variable but a function using an arrow function called get products alright now this is a controller function which is going to be hooked to our route so this is gonna take in the same thing response and we want to set this to the type of an object with response any and then we can take take that response and say dot body and what we want to return here what I like to do is return an object with a success value and this is gonna be a boolean in this case true and then a data value with whatever the data we want to pass back which is going to be all of our products now we need to export this so down here let's say export get products and then in our routes will import it so we want to import get products from our controller and then down here we can just replace this whole arrow function with the get products so if we save this and restart this server looks like we're getting it oh I forgot the dot TS that's something I keep doing because it can be J s or TS okay so that's running now we'll go ahead and hit that road again and we get 200 response we get success true and in our data we have an array of all of our products which is exactly what we want now I'm gonna go ahead and just create all of our functions here and export them because I have a tendency to forget to do all that stuff so let's copy this and let's paste this in and then I'll just get rid of everything that's actually in here in the body so this this function is going to be to get a single product so we'll call it just get product and let's change this to say get single product and yeah so we'll just copy this so we'll get a single product and then we'll have one two I'm sorry this route here has to be products slash and then ID like that and then this one is going to be to add a product and it's going to be a post request to that same endpoint and the function will be called add product ok we'll paste this in again and then this is going to be to update a product which is going to be a put request and it's gonna be two products slash and then an ID per am and we'll call this update so I'm just creating these first and then the last one will do is it's going to be the delete so this one will be delete product which will be a delete request to products slash and then the ID and we'll change this to delete okay and then we just need to export these so we have get product we have add product update and delete and let's go to routes and import all that stuff get product add it should pop up here so update and delete and then we'll create our routes so we could just we could do router dot get for the next one which is gonna be products slash ID and that's gonna call the get products which will get a single product now we don't have to keep doing router here we can just tack on dot get like that so we have that and then let's do our post requests so post is going to be just two products and that's going to call the add product function then we have our update which is gonna be a put request so put and then update and then delete so delete by ID that's right and then delete product alright and if we want we can just do a request body just so we can test out the routes so for instance here we'll just say get here say add just to make sure all the routes work and stuff I just want to get this out of the way before we do the logic and then this will be delete all right so let's restart this and let's go here and if we open up a new tab let's copy this and if we do slash one we should see yeah okay we haven't created the logic to actually do it but I at least want to test the routes so the next one I want to test is let's do update because our put I should say because that needs an ID ok update good delete okay that works and then post doesn't have an ID so we want to get rid of that and add so we at least know that the routes are working and the functions are being called now we can work on the actual logic so let's start off with we already have get all products let's do get single product so for this we're gonna need to grab on to this ID that's passed in so in addition to response we want params okay and then over here we'll say params not any params is actually going to be an ID so it's gonna be an ID and it's gonna be a string and then down here let's get rid of this and figure out how we want to do this so if you were using a database like if you were using Mongoose for example with node you would be using Mongoose methods like find and create and all that stuff now we're just dealing with static data so we're just using pure JavaScript functions well for instance we'll use the find hi or array method here so let's say Const product because we need to get a single product and we'll set this to our products array and then we'll use dot find which takes in an arrow function so we'll say for each product we want to get the product where the ID is equal to the params ID and we're using typescript so we can add a type here of either product or undefined okay so whatever this is has to be formatted as a product which we created the interface for or undefined if it's not found now down here let's have an if statement let's say if the product is found because I mean they might put an ID that's that's not there if it's found then we'll set the response status to 200 and we'll set the response dot body - an object with success true and the data is gonna be the single product that was requested else oops that's not the right place right here else then let's go ahead and set the response status let's set that to 404 cuz it's not found and then we'll set the response I'll just copy this actually so we'll set this to false success false and then instead of data I'm just gonna put a message so let's say MSG and we'll say no product found so let's save that and we already have everything exported and all that stuff so we can just restart this and let's test it out so we want to make a get request to products / and then let's do one and there we go we get success true gives us our product now if we do 11 which we know there's no product with 11 we get false 404 no product found ok so that's working correctly now let's do the add product ok so we want to be able to add to that array and return it now to get data that's sent from the client we have to pass in here request so we want request and response so over here let's say request which is going to be any ok and then in order to get the the body the data in the body from the client let's create a variable here and we get it from request dot body so this body method which returns a promise so we need to use a wait oops want an equal sign here now notice I have a squiggly line here even with the demo extension that's because this is no longer in the global scope or the top-level so we do have to add a sync here like that now I want to just make sure that there's data sent with the body so we can say if not request which has a method called has body so if it has an body data this will be true but we're checking to see if it's false if it's false then let's go ahead and send a response status of 404 which means that they they haven't formatted the data correctly or given us what we want to get a correct response and then the response body is gonna be success false and then we'll have a message of no data okay and then we'll have an else here else then we want to add a new product so I'm gonna say Const product and we want to get that from the body dot value because this body right here once we get it from here is going to have a type and a value so when we send our data it'll be in JSON format so the type will be Jason and the value will have the actual data so The Vow the value we're going to assign to this variable which should be formatted as a product okay and then we want to create an ID because even with most api's you don't create the ID from the client it gets added by the database however we're not at we're not using a database so what I'm gonna do is bring in up here at the top I'm gonna use the UUID standard library ok so there's an object called v4 because it uses version 4 of UUID and we can bring that in from HTTP and then denno dot land slash STD slash UUID and then mod TS alright and then we can use v4 which is an object that as a method called generate to generate an ID so let's go back down here this will include this will include the name name description and price and then we'll take that object and add an ID to it using v4 dot generate ok so that will create the ID so once we have the product constructed we'll go ahead and take the products array and we'll push on to it the new product and if you are using a database you wouldn't actually have to do this much work you wouldn't have to manually create the ID and push it and stuff like that you would just have like products save or something so now for the response let's do a status of 201 which is which means everything went okay and something got created and then the body will be success true and the data will be that product that was just created so let's restart the server and let's try that so now I'm gonna make a post request to just products now if I don't send any data I get false no data with a four oh we don't want 404 actually we want 400 404 is not found this is you are 400 here and then yeah so I'll restart that so we get four hundred bad requests now to send data we'll go to body raw I'm gonna choose Jason and let's send a name name you guys see this smooth for some reason this isn't I can't make it bigger well I'm just putting an object adjacent object with a name I'll say product for description this is product for and price $19.99 all right so we'll go ahead and send that and we get back to o1 created success true and we get back the the product we created and you can see the ID the UUID was generated and put there as well and then if we go back to this tab and send again you can see that product 4 is added all right so we're now able to add a product now if I restart the server it's going to disappear because it's only in memory it's not saved in the database so now let's do the update so with the update we're going to need we're going to need params we're gonna need a request and response so over here let's say params which is going to be it's going to have an ID which is a string and then after that we have request which is any and response which is any so for the update it's going to be very similar to where we got a single product so I'm gonna copy I'm actually gonna copy the whole thing here so getting this to the product by the ID and then this if else and we'll put that in the update here so we're getting the product by the I by the params ID and then if that product exists let's see then we want to we'll just get rid of this so if that product exists we want to get the new data from the body so let's say Const body a weight request body and we have to mark this as a sink and then the updated data is gonna come from body dot value so I'm gonna actually just create a variable called update data and set that to body dot value and as far as the type goes this should be formatted as an object with an optional name which is gonna be a string a description which is gonna be a string and a what else price which is gonna be a number all right so the updated data whatever sent will get put in this variable and then there's a lot of different ways we can update I'm gonna use map I like this way it's very clean so we're gonna say products equals and then take our products array and map through and say for each product we're going to let's say we're the product ID is equal to the params dot ID then we're gonna set we're gonna use a spread operator here and take that specific product so basically just mapping it to that specific product and we're gonna add the updated data so we want to use the spread here and use update data okay else so this is a ternary so we want to say else then it'll just be the product so it's gonna map through every product and if it matches the ID that's passed into the params then it's going to add that updated data to that specific product so hopefully that makes sense and then like I said there's a lot of ways to do this now response let's say response status is gonna be 200 it means everything went okay and then response start badi say success true and then as far as the data goes we'll just we'll just return the entire thing so the entire array with the updated data and then we have this else here so this is basically if it's not found and we can just keep this as is because up here we're just checking for the product if it's not found we'll send that back so let's try it out save that and reload so now we want to update so I want to put put request and let's update the first one so we'll do slash one and then in the body here let's change it to updated product and we'll change the price to let's say 1 9999 and send so we get back all of the products and the ID of 1 you can see the name has changed and the price has changed the description is the same though and if I go back to here make a get request to get all products you'll see the updated product which obviously won't stick if I reset the server but if we had a database it what we would have updated the database ok so that works now we just want to do the delete and we'll have a full crud REST API so for the delete let's see we're gonna need params cuz we need to know which one we're dealing with so here we'll say params and for type let's say ID which is gonna be a string so the delete this is gonna be pretty easy we can just use filter so let's say products our products array I'll set two products dot filter and we'll say for each product let's return where product ID is not equal to the params dot ID so that should give us all the products except the one that we want to delete and then we'll set the response body let's set that to success true and let's see I guess we'll just do a message yeah we'll do a message product removed so I'll save that and let's restart our server okay we'll go back here so right now we have three products right so now I'm gonna make a delete request two products slash one and send we just get back product remove I mean we could send back the whole array if we wanted to it doesn't I mean it's up to you how you want to structure your API but now if I go here and send you can see that product one is now deleted alright so now we have a fully functional crud REST API of course everything is just in memory we're not using a database or anything but I will be creating a video where we implement a database into this API but as you can see I mean if you are familiar with node and Express this isn't much different in terms of how you do things I mean we just have you know our main entry point we have our routes connected to controller functions the way that we bring in packages is obviously different and there's no node modules folder here with a million different packages in it so I mean I like it I like demo again though it's very very new and nodejs is far far from dead so don't feed into that hype or that but that's it I hope you guys enjoyed this I will have the code for the API in the description the stuff we did earlier I won't just because it was it was just to kind of show you some of the fundamentals but I hope you enjoyed this crash course and expect more videos on demo in the future thanks for watching
Info
Channel: Traversy Media
Views: 126,069
Rating: undefined out of 5
Keywords: deno, deno.js, deno node, deno crash course, deno traversy media
Id: NHHhiqwcfRM
Channel Id: undefined
Length: 52min 5sec (3125 seconds)
Published: Tue May 19 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.