NestJS Tutorial For Beginners - Learn NestJS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys how's it going I'm back here with another video and in today's video I'm going to be introducing to you guys one of my favorite Frameworks in no GS which is also well known called nestjs I'll be taking you guys all the way from how to install this framework into your node.js application and how to actually set up with a example project I also Cherry Picked a couple topics uh within the framework that I think are important to understand uh that deviate a little bit from the uh only beginner stuff because I want to prepare you guys to build applications with the library so if you want to check out all of the code we're going to be working on will be in the description and yeah that's that's pretty much it um this video is not sponsored however if you're looking to deploy or host any application either it be a front end or a backend um I definitely recommend the friends of the channel uh hostinger I'll put a link for them in the description if you guys check them out um it could help me as well so I always want to promote them because they've actually done so much for this Channel and yeah pretty much that's pretty much it so let's get into the [Music] tutorial okay everyone so first things first uh let me just go and give you guys a quick explanation of why you would want to learn nestjs by the way it's nestjs not nextjs a lot of people get confused between the two but uh nestjs is a framework for building scalable node.js applications so it is mostly a backend thing and it is good because in my opinion it actually enforces really good coding practices so you'll see that it basically fully supports typescript and it utilizes elements of object-oriented programming and the MVC programming model in order to write your application the whole point of the framework is is to facilitate developers to build a really scalable nodejs application by solving the problem of how do you architect and structure your application so you guys will see that after we install it um they have a really nice CLI that actually facilitates creating the whole application for us so let's get started with that okay everyone so let's get going with installing um this the nestjs CLI inside of our computer so since we're installing a CLI we have to install it inside of Entire Computer so it's not necessarily for this specific package so I'm just going to install by using npm i-g being Global and then at nestjs SL CLI I'm going to press enter it's going to install the package I've uh installed it already um so I can test to see if it's installed by running nest and then uh Dash V and you'll see that um it shows us the version that is installed on my computer so if it didn't if it wasn't in your computer it wouldn't even show this so that's how you check if it's not actually showing up and the it says that it couldn't be found as a command try closing your terminal and then opening it up again so now that we are here we need to create a new nestjs project so in order to do that we can just use the CLI and say Nest new and then we can put a name for our project so I'm going to call this book API now as an example for you guys I'm going to be using I'm going to be creating an API which is a very simple crud for book information um it's going to also ask us which package management we're using I'm personally using npm in this video so I'm going to choose npm um it's going to install start installing everything um you can see already over here that it's already created a bunch of files for us um but uh we'll wait while the thing says installation in progress and I'll be back in a second when it's done okay as you can see uh it just finished up uh and I can just command K to clear up all of that and let's take a look at all the stuff that is new inside of our application so obviously created here inside of the book API folder uh a bunch of files now I'll explain what each of these files mean so the first one that you need to understand is the main.ts file this is the entry file of your application and it utilizes The Nest Factory um core function to actually create our application and then it basically tells um nojz to listen to the port 3000 which if you've worked with nodejs in the past um this will actually start our server on the P 3000 um we can actually run it um if we go to package.json uh they already created a bunch of scripts for us now in order to run it we have to first CD inside of the book API folder because now we want to be inside of this folder and inside of here we'll run npm Run start as you can see it will um run the actual command um and create the disc folder because what happens is everything is we write everything in typescript as you can see by the TS files but it obviously um compiles everything into JavaScript because that's what is built in the end and served to our um computer right so as of right now you can see that our application is indeed running so we are good enough to start talking about the next few files so the next one I want to talk about is the app. service.ts so this is an example for how a service would be created in um nestjs so if you're not familiar with a by how how nestjs is organized basically the idea is you'll have controllers which will handle incoming requests and they created here with the CLI uh bootstrapped um code um basically created this app. controller. CS as an example for us the controller will be where we be handling all the incoming requests as an example here we'll be handling uh an a get request with a route that is called get hello just as an example that they made for us the service uh is the part of our application where we're going to be handling the data access and we'll encapsulate any business logic so in this case over here you can see the controller is calling the ser service and in the service is where we would probably make some request to get data handle that data and then return and then in the controller we would either do something with that data uh actually in this case just return it back right then we have the modules and the module is basically where we organize The Code by grouping related components together so we'll create a module over here which is the global module inside of our project and here is where we list out all the Imports all the controllers and all the providers which is all the services so it's a way for us to get all the different controllers and all the services that we have in our application and just group them together to then pass them into our uh main.ts and create our Nest um application so as an example for fetching the data in this tutorial I'm going to be using insomnia and if you've never used insomnia in the past um it basically allows you to test out um API requests um you there's multiple other services out there that you can use as well um there's Postman that is really famous you can actually build a front end for this if you want to and just test that out there but as you can see um our service is running so if I make a get request to the um Local Host 3000 we'll get returning back a simple message or string saying hello world which is expected because this is what we're actually doing right so now that we have kind of established um how we're going to test this and the initial idea of this I want to just go over um I'm going to delete this over here um right off the bat and I want to also delete this one over here and we are going to um actually I might leave them just as as and we will alter them um as we go so what we want like I said is to create a book API so we're going to imagine that we're working with a database um I'll show you guys exactly where you would make the database calls but we're going to have a file inside our local system a Json file with um some data that we're going to manipulate as you can see over here I created a file called fake database and um this is just for testing purposes I'll even Link Link all of this in the description um basically um I Define the structure of the data we're going to be playing with which will be a book and I also created a simple data set for us over here right so book will consist of an ID a title an author and a publication year and I listed out over here A bunch of examples um being honest I got all of them from CH PT um but this is what we're going to be playing with in this tutorial so how would we start generating this API using nestjs so the first thing we have to do is Define our controller right so over here you can see we had the initial controller for everything right and I can actually change the name of this controller to actually more properly match the API we're working with app controller is very generic right and um it's what they give us but we're actually going to be working with books so I'm going to call this book controller or books controller maybe perfect now we can actually give a name to this controller that will exist inside of the routes of the API that we call so for example when we pass this without putting anything inside of this controller DEC decorative over here um any request to Local Host without putting any extension to the URL will work but if I came over here and I said book just like that um we would access all the routes inside of here by first putting making a request to the book or books actually um extension of the routes so this over here is important because we definitely want to separate our routes based on what controller we're working on um now that we kind of changed the name of this I also need to change it on the module because we used to have the app controller we don't have that anymore we have the book The Books controller now over here we want to have some routes right off the bat I want to create a route that is basically just going to return to us the list of all the books that we currently have in our database so um we're going to create this function called get all books and what I want this to return is a list of books now if you look in our fake database over here um we haven't exported this interface and if you're actually working with mongod DB or with um I don't know M MySQL or any other type of database management system that you're working with you definitely need to create um types and match them to the types that you're going to be working on your controllers because it's everything using typescript right so over here I can very easily say that uh this endpoint will return a list of books I just have to import this so I actually say um fake database and I'll import the um book U interface right um I also just realized I was being stupid uh this is how I Define the array type um and we can now Define that this endpoint will return a list of books now we have this little thing over here at the top right similar to how we had for the controller and this will help us tell nestjs what type of Route this is now we're saying that it is a get request now if we don't put anything inside of here making a get request for um Local Host 3000b books should actually return whatever is inside of here as an example for now I'm going to comment out this call for the service because we're going to handle that in a bit and I will return um just the word book just like this oh I also realized we can't do this right um I actually change this to be a string for now just to show you guys um and I'll return the word book now will this work well no it's says cannot get books because even though we made some changes to our API we ran our uh our project using npm Run start which actually doesn't refresh um the server or restart your server every time we make some CH changes to the file if we want that to happen we have to run npm run start Dev and then every time we make any changes to our file it will actually restart the server for us so opening up over here if I make this request you'll see that it will return back the word book and we didn't even have to put any extensions over here right but if I wanted to for example to get all the books um I wanted to have to come over here and make a request to slash books slash get all books I would actually have to be a little bit more specific and the way I would do this is by putting that specific string over here so I would say get all books and then over here if I run this it would actually work but for all purposes is for this endpoint I actually don't want that I just want it to be the most standard get request to the books route and you can see that it continues to work so now let's go back to what we actually want which is to return a list of books now how do we do this well we want to communicate with our service because a service is where it's going to make the call to our database or our case our fake database so we're going to instead of make a call to the app service um which exists over here we're going to change this to the book book service because now we're changing the names right and since we did that we also have to change it here to books service and pass it as one of our providers now in our app. controller we have to make a reference to that service so I'll change this to books service um not Services service and I'll even change the variable to books service here as well now in order to make a call to the new uh renamed service I'll just call this this. books service and you can see it still has the get hello function that comes with our um initial project so let's actually change that let's call this get all books and say that it returns a little list of books that we'll have to import um again from the fake database now over here is where we write the logic where if you were using mongodb you would um do some sort of database request or if you're using my my SQL you would do a query um this is where you call your database right um in our case we're just using this file as a fake database like I said so what I'm going to return here is just the actual books array so I'll return books and since we're returning this on the get all books function of our service and we're getting that inside of our controller by now saying get all books over here we are returning from this controller the correct data you should see that now if I make this request we get back the list of all the books now you might be thinking why didn't we just return books over here right we could have easily done this causes the same thing like it literally we're just creating extra functions right for the same purpose well the reason why we're doing this is because this example is very simple so in this example we really didn't have to do any major things on our controller so inside of the controller you could handle a lot more stuff you could handle get getting informations about the request like getting prms getting headers getting stuff related to the request in itself you could do stuff like validation of the data that is coming back from the client you can also return back statuses and error codes stuff that actually relates to the request in itself and also here is where you would handle or at least call authentication to um see if the user who is making the request is the user that should be making the request all of the other stuff which is related to data should all be handled in the service so none of that goes here all of that goes inside of the controller since this thing over here this endpoint only calls um the only purpose of it is just making a request to the database there's no much added layers to it it looks kind of like we're just doing this um for the purposes of just separating it but in the long run this matters because we're abstracting our knowledge and separating our concerns and it's the main point of how nestjs architecture is defined find now let's make some more uh interesting um requests so the next endpoint that we want to work on is an endpoint that will return to us a single book if we give an ID to it so it will be a little bit different from the get all books because we're not returning back a list of books we're only giving back one and this time we will need to do some stuff and some logic inside of our controller so let's get going first of all it is a get request so we we have to specify that now since we have another get request over here um this one will need to be renamed because we can have two requests that have the same name technically having no name is having a name when you're working with um routes so since this one is a little bit more specific I'll give it a name of get by ID and we can test this by putting get my ID over here now I'll save that and we can start working on um the function so the function will be called get book by ID now it is a little bit different because like I said we will be returning a book but there is layers inside of um this uh parameter over here because we do need to grab the ID from the user in the client so we are expecting the user to pass in that ID in some way or another and there's multiple ways you can can do that you can pass IDs through the body you can pass ID through the through the headers but the most common and obvious way of doing it is by using query parameters so I'm going to be grabbing a parm from this controller the way I Define that is by using the PM um decorator over here and I can put a name for that progam I'll call it ID now I have to give a type for that IDE for that Pam which obviously is a string but also we have to Define how the Pam will look inside of our route so we have to actually put the Pam over here just like how you would in any other API now if we were to come over here um actually let's not even test it now because um it's not going to work uh but if we wanted to make this work we' have to put a slash I uh one over here for example and this is how our actual right route API call would look like now we have defined everything for this route um this controller we just have to um write the logic inside of here good thing is that it's really easy to get the peram from this request we can create a variable over here called um book ID and set it equal to plus ID and this will basically give us back whatever the user puts as the Pam over here and now since we have access to the book ID we can return a call to the books service Dot and then we have to create the actual service function for this controller so let's go over here and let's create the service function we'll call this find by ID since this will be the actual one that we'll be calling and it should return back a book or undefined now the reason why we're putting as undefined is because possibly this will return undefined if someone is trying to find a book that doesn't exist in the database so um we'll have to account for that now we also can put over here a book ID as an argument to this function and it will be a number and um we can write some logic inside of here like fetching the data from the database and passing in the ID in our case to actually get this we'll just say books doind and then we'll Loop through it book um just like this and we'll return the book which has the ID equal to the book ID that we passed in the argument of this function now obviously you can return undefined and defined function as you can see from its type it can return as an undefined so um this should be good if we come over here and call the find by ID function and pass in the book ID variable now I also have to account that this can be undefined so I'll put this over here so perfect we have this um route defined and the service working so let's try see if we can get the to Killa Markin bird book by passing the ID of one um it seems like we couldn't um it says that um cannot get books SLG get by ID um and then one I think the reason for that is because we do have to put a slash over here uh as you can see putting a slash Works um sorry about that uh we have to put a slash to separate the name of the route from the parms so now we can see that this could work for any other one of the books if we change the ID it will actually return the book relating that ID perfect now we finished the two get requests that we have and we can start doing um some more data manipulation like working with a post request cuz post request is a bit distinctive because um it requires some data that we can get from the body of the client so we'll Define a post request over here and this post request will be called add book now it should return back a book because the whole point of it is the client will call this uh endpoint will pass in a book data uh through the body and then at the end if we successfully actually create created the book it should return this if not um we can handle something like an error message or something like that so this is perfect um for now we can just start defining um a little bit more of this adbook function for example if we want to get the body of a request we have to do something similar to how we did with perms we Define uh decorator called body and we give it a name so we'll we call it book and this will be of type book so from the front end um we're expecting this to have a type of book now we want to be able to generate the ID inside of here so I'm actually going to say that the type of this is partial book because um in typescript um if we don't do this it will require us to pass a object through the through the body of this request that includes the ID already but to be honest we want to generate that ID inside of here automatically so my expectation is that when we make this request we're going to send a Json object which will include the following things it will include a title which as an example here let's add the book Harry Potter and the Sorcerer Stone um let's give an author uh it's JK rolling um so JK rolling uh publication year let's put um I think it was [Music] 1995 I'm not sure but this is kind of how we want to send the request so no ID is going to be passed and obviously over here let's change this to be a post request and we're not going to put any specific uh extensions we want to just make a post request to the books endpoint because um I would assume that the most generic default endpoint route for books would be to actually just add a book right so this is kind of what we want obviously for now it doesn't work but what we want to do is I want to come over here and I want to get the book body so I'll say book uh data equals to um the book that we're getting from the body we don't have to put a plus like we did with the par with a body we can just do it like this then we want to generate new ID so there's multiple ways of doing this um I actually thinking about it right now I might do this in the service and you guys will see why so let's actually just return over here this. book service do um add book Let's create that function and pass in the book data over here now the ad book function like I said will actually be one of the easiest ones to make we'll say um actually let's not call it Ed book uh let's call it create because we want the service functions and stuff to be very um no not this uh this to be very generic to to handling data you know we might use this um reuse this more than once so we want to keep it generic to um what database operation or data handling operation we are actually executing so over here we'll create the function we'll say create and this function should return back a book and should take in a book as the argument which is a partial of the book type now inside of here we want to actually generate the new ID now for simple purposes like you saw from the data each ID is just one plus the last one so I'm just going to get the last item in this in this um fake database books array um I'm going to get the L one so I'm going to say books. length minus one and then get the ID from that and then do a plus one so we're just adding something to the end of that list so we're just adding a new ID like that then we're just going to say books. push a book a new book that will be of the format of the book that is sent through the body plus the ID of it will be equal to the new ID just like that and um there's an error type because it says that um the title the um author and the publication year is optional however um we're kind of guaranteeing uh we could obviously uh handle it in multiple ways over here and our controller this is where we would actually um handle for example if um if no um book. author then return something like an error message if no book dot actually I'll make it for all the other ones too so not book. tile or notbook Dot publication here right we should return undefined or an error message I'll put undefined for now um and I'll say that this also returns in fine um for Simplicity purposes because now what this means is that um we're kind of guaranteeing over here that um the other fields are present so we can actually say that this will run if um we have book. author and book. publication year and book. tile perfect because now we can encapsulate all of that over here and just push that cuz now over here if we wanted to generate the object I'll actually created over here at the top I'll say const new book equals to an object which has an ID of new ID like we did below the author uh and I'll actually say that this is a book as well the author is equal to book. author and we're kind of guaranteeing that it is um true in the controller but if anything for typescript safety purposes we also default it to an empty string then we'll say title we'll say book. tile and default it to an empty string then publication year and we'll do this same we'll say book. publication year and we'll default it to zero because it's a number now over here we can just pass in the new book perfect now you can see the error is gone and we can actually um return this book so um we can just say return new book over here and everything should be perfect and yeah at the end we can just um change that so let's try this out now let's go and make this a post request we'll again try to send this data when we click Send we see that we get back a book similar to the one we just try to create the same fields and with the ID of 11 if you recall we had 10 items initially now we have an item with ID 11 so the ID was correctly generated now how do we actually test to see if our data was manipulated let's make a request to books and make a call to the get all books controller endpoint when I send this you should see that we have all of our previous books plus the new one that we just added in the end which is perfect means everything is working so we're pretty much done with our post request let's continue rolling and go ahead with our update so I'll say update over here actually put it doesn't have to be update um it's a put request and for the put request we'll also probably just keep it lowkey with a sense like we won't um put any specific like route names um we do have to grab um the way we're going to change the field which by the way the the field that we're going to force this to change is the way we're going to update this book is by passing in the body like all the fields we want to change and um an ID for the book that we want to alter so we since we're working with ID we'll do something similar to what we did over here and we will work with some parms specifically the ID PM so since this is a put request and we only have one in our um controller I'll just name it the default route then over here I'll call this update book and it will in the end return back the book that was updated so we'll say book um or undefined perfect now this will be very similar to this uh we'll actually just straight up copy this and put it over here cuz we do have to grab the ID PM but also on top of that we also need to get the body so we'll say body over here and we'll grab the book of type book or in this case it will be of type partial book as well because it can be actually any variation of a book it can be just the title field it can just be the author field it can be just a publication field um it can be just one of those fields that we're trying to alter and perfect now we want to return all of this by saying this. bookservice Dot update and we want to pass an argument over here both the ID and the book we want to pass both of them so that our back our service has all both of them to make the query to our database now differently from what we've been doing so far of grabbing the data and the structuring everything immediately let's actually just pass everything directly to the arguments um let's assume that this update function we're going to create in the book service is going to have the first argument being the ID and the second one being the book and then in the book service will actually create it so I'll say update over here and I'll say that it returns back a book and it's undefined and down here we'll just say that the first argument to this will be the book ID it will be a number and the second one will be the updated book and it will be um actually book fields and it will be a partial of a book perfect so how we're going to structure this is a bit different from what we've been doing so far we're going to first have to get the current data for that book so we do need to find the first the the current book so I'll say const um find I'll just actually copy this um books. find and I'll create a current book equal to books. find and pass in the book ID over here and this will return back how the book currently is before the update now we also have to Define how the book will look um after the update so we'll create a a few a an object over here called updated book and what we're going to do is we're literally going to set this equal to a merge of this object and this one over here so the merge will basically be we D structure all the updated book fields because this will be an object with maybe one maybe two maybe all three fields that we can have in a book um and we'll just put all of them directly over here and then whatever is left that is necessary in a book we can actually D structure from the current book itself so what this will look like is if the updated book fields if we pass in just the title just like this a new title as the body to this request um the current book object will be the full book with a title the author um I won't write everything but it will have everything over here right and then the updated book object that we're creating right now will will start off being just an object with a title which is we're getting from the up the destructuring of the updated book fields and then we're going to fill out this object with whatever is left in the current book that isn't in the updated book fields object so it's actually a very easy way to make this work and in the end we can actually just um say um books. map because map will replace an item or multiple items inside of this list um we have to just first um find um the the actual item so I'll say book over here here and I'll just say that if the book. ID is equal to the book ID that we're trying to alter then I just want to turn that into the updated book that we just created if it is not so if it's all any other book that we don't want to touch we'll just keep whatever the current value is right now so we'll return book just like this so obviously this is a little bit complicated but this is just because we're using a fake database um array um it will be complicated in itself if you're working with any database system um so now in the end over here we have updated that thing and we'll just return back um the updated book perfect and this should be working let's actually try this out let's change this into a put request and um for the ID you equal to 11 right so the JK rolling book this one that we just added the Harry Potter and the sorcery Stone I don't know if you guys know but there's two titles to this book actually there's Harry Potter and the Sorcerer Stone and there's the um the US version which is the philosoph first stone now imagine I just want to change that I don't want to change anything else well I can very easily now go ahead and do this and you'll see that it returns back um this information there might have been an error because this should have returned back um the full book so uh let's take a look to see if it actually if it actually um screwed something up let's say get over here and let's take a look to see if how this looks in the end um it seems like it's not even here anymore because it doesn't actually match the data format because we actually removed all the other fields so what I think we can do with this is we can actually change this a bit and say that the updated book will have an ID equal to the book ID so we'll just Define everything immediately I apologize for the mistake um we'll say ID is equal to book ID um then for the title it will be equal to um updated book fields. tile or the current book. tile so what we're seeing here is um we'll first try to put whatever is on the updated book fields if nothing for the title field was passed to that object then we'll just keep it how it was before and we'll do the same thing for the other ones actually I think this actually might look easier to understand and I should have probably done this since the beginning but as you guys might know if you've watched my videos I like leaving on mistakes because it showed you guys how my thought process works and it helps you guys understand better so I'll put over here public here um and do the same um close this up then publication year and then publication year perfect and now let's test this out again let's try to get all the books right perfect now let's actually that example I created completely screwed up let's just change Brave New World to coward New World um as an examp example the ID is 10 so I'll put 10 and let's change the title to um coward New World send as you can see it seems to have worked we get back the same book full object with the updated title now let's do a get request over here and just get that book and let's see if the title has updated it doesn't actually seem to have updated now with this current book I want to create a new updated book and it will be pretty simple first of all the ID of this updated book will be equal to book ID because we're already passing that in the argument of our function then the title will be equal to updated book fields. tile now this can be null because we might just pass in the author to update we might not even pass in the title so in the case where the title is null in this object we actually want to default it to whatever the current Buck has and we'll do the same thing for all the other fields we'll do it for author so we'll put author here and here and we'll do it for publication ear and we'll do it here in here perfect now how do we edit this how do we actually set the current value to the correct one well first of all the ID that we have um for this book is is equal to 1 plus the current index in the array right because this is how we're defining our array the first item has an index of zero and idea of one so we can actually very easily just alter this array by saying that um books um at the index equal to the book ID minus one is equal to the updated book and then in the end we just return back the updated book perfect now let Let's test this out let's come over here um I have over here an example that I've actually already created it's uh we have the list of books right that we have in our database and in the last book we have Brave New World right so I want to change the title to coward new world so I would make this into a put request and I would put the ID of 10 which is the IDE of the book at the parm of this routes then I would pass in an object with just a title field because that's the only thing we want to alter and with the the new value if I click Send we should see the new book over here but did it actually alter the list of books well let's find out let's change this to a get request make the request and as you can see it did alter but let's see if we can actually update more than one thing at once let's make a put request for books and actually change Fahrenheit 451 um which is a book with id9 to have a title of Celsius um 154 right and also the author let's change the author to be instead of being Ray Bradbury let's change it to be Pedro Tech couldn't come up with anyone else let's see if we can change two fields at once seems like it worked now let's go to get and actually get all the books and it seems like it worked as well perfect our update functionality is working let's go to delete so we're getting actually pretty fast at this because uh once you get the hang of it it becomes way easier and for the delete to be honest it's always the easiest one to work with other than the get uh in my opinion um the delete honestly should just return back the let me think it should probably just return back the actual list of books that we we have with the delici so let's return back a book array um as the return from this function we also need to put the decorator right so we say delete and to in order to delete a book we need the ID of the book we want to delete so we also have to grab perms here so we'll say ID and over here we also have to put the Pam decorator and we'll put ID perfect now inside of here let's um return thisbook service do delete and assume that we'll have to pass over here the ID as a as a argument to this function right and I also forgot we also have to put the ID over here inside a value of string so that we get rid of all that those errors now inside of here let's create the delete function like I said it will take in the book ID which will be a number and it's actually going to be very simple we're going to return over here the book array but the way we're going to do this is how else would you delete an item in an array with a certain ID well we have the filter function which exists in JavaScript which makes everything so easy we can just say books do um books equal to books. filter so we we'll set the books array equal to a copy of itself but we'll filter through that copy and delete whatever book has a book ID uh equal to the book ID that we passed in this argument but the filter function basically it's the opposite it keeps whatever satisfies this condition so we want to keep all of them in filter for only those which doesn't have the book ID equal to the book ID of this function right and then in the end we'll just return books um it seems that we cannot assign books because it is an import so hm to be honest um it makes sense I should have just defined this array instead of the app service because we just can't mutate it um so ignore what I said about the filter there's we'll probably have to use the other method which is splicing based on the index so the way we would do this is I'll probably have to come over here and say array or books in this case do splice then put the index of the item that we want to remove right so the item we want to remove is the book ID minus one same logic to how we did this over here and we also want to want to remove one item so I'll put one over here and again let's just return books at the end this actually looks way simpler so let's test this out let's change this to a delete um operation and let's remove the body nothing is necessary and I want to remove the Celsius 154 um book so I'll put nine over here click Send and it seems like it has removed because it returned an array with everything but that specific book now this is pretty much it and it's basically most of what I wanted to teach in this video I really want to make an extension to this video if you guys are actually interested I've never made an sjs video in the past so I'm I'm just gouging the interest from everyone where I go over more complicated topics my thoughts is I can talk about pipes I can talk about filters guards um middlewares or interceptors um how to do authentication all of that kind of stuff maybe even build a project with nestjs um but this would obviously be out of the scope of a beginner video um the last thing I actually want to show as part of this beginner video is some really simple and helpful commands that we could have used in this tutorial like the CLI facilitates if you notice we actually created everything instead of the app. service and the app. controller file I realized we changed the name of the controller and the service to books but we never actually Chang the name of the file right if we want to generate it very easily we can just use the CLI for example IM mention I want to add a part of my project that deals with movies instead of books I can just straight up come over here and say Nest um G to which means generate then specify that I want to generate a controller and give a name of movies press enter and as you can see it has now generated for us um the movies controller but I realize I'm outside of the actual of the actual uh directory that we should be I should CD into books API and then run the same command and you'll see that this will now be created inside of our source folder right um now what if I want to create a service for that well change the to service and you'll see that it does the same thing um it adds the movies service to this um it puts the name for it it puts the name for the controller um it does everything automatically we can even create a module right I could actually come over here and change this to module and you'll see that it will uh no let's not make it flat I'll press enter over here and you'll notice that it created the module file for this as well so like I said um nestest is really good because it enforces great um code standards and abstraction um it it actually emphasizes objectoriented programming because we're working with everything in classes and defining our methods in the the way that you would in object-oriented programming and um it supports MVC right we're separating the the concerns um of our application the business logic from the controller logic from where we view our data from everything where we're just putting everything in their specific spot and handling them separately when everything connects in the end making our project more scalable and easy to implement so that was the point of the video I really appreciate if you guys could leave a like And subscribe massively appreciate that again check out hostinger this video is not sponsored I'm guaranteeing that um but I really like their service and I do get some support if you guys check out their link um because I've used their service and I've been sponsored by them in the past so if you guys could check that out I would massively appreciate it um with that in mind thank you for watching and I see you guys next time
Info
Channel: PedroTech
Views: 7,511
Rating: undefined out of 5
Keywords: computer science, crud, javascript, learn reactjs, mysql, nodejs, programming, react tutorial, reactjs, reactjs beginner, reactjs tutorial, typescript, react js crash course, react js, node js, express js, pedrotech, traversy media, traversymedia, clever programmer, tech with tim, freecodecamp, deved, pedro tech, nestjs, nestjs tutorial, nestjs microservices, nestjs tutorial for beginners, next js beginner, nestjs beginner
Id: BCl0p5gZ1yw
Channel Id: undefined
Length: 51min 49sec (3109 seconds)
Published: Wed May 15 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.