Node.js RESTful API CRUD with Express & MongoDB

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi all welcome to code affection in this video we'll discuss all you need to know about credit operations in node.js API with mongodb I am sure you will find this tutorial entertaining so please watch till the end of the video as a follow-up to this video I'll be building the front-end client-side app for this same API in Frameworks such as angular or react or even JavaScript render engines like handleboss so let's get started with creating the Nord project inside this folder directory here for that we have to open the command prompt from this same folder to do that inside this file explorer path directory just replace this path with the command CMD then hit enter thereby command prompt is opened from the same directory as that of this folder here now first of all let me create a new folder called server so here we have the new folder server now let's navigate into the folder server inside the folder we can create the node.js project using this command in PM in it during this process it will ask some questions for initial configuration of the application first of all we have the package name inside this application we'll be dealing with employee details so the package name would be something like employee API keep the motion as it is no description entry point which is the root Javascript file for the node.js project I will keep it as index.js keep hitting enter if you are okay with default values so here we have the configuration that we have just provided and it will be saved inside this file package.json now to confirm this configuration press y for yes hit end up that's it now here we are inside this folder server now let's open the same inside the vs code editor for that we can use this command code then period boom so here we have the brand new node.js project now before going forward let me show you the folder structure of the application that we are going to build so I will create this text file Here app structure and then I will paste this app structure here so inside this project we'll be having folders controllers middle ways models and services and here we have the root Javascript file index.js and there will be a separate Javascript file DB in order to establish a connection with the DB now let's create this root Javascript file index.js which is the entry point for this application so here we have the new file index.js now let's install the required npm packages into the application first of all we have not mon it helps us to restart the application automatically whenever we make changes inside the project directory now you can see the corresponding installation command here now here is the catch instead of installing this package only to the current project install the package globally with this command here and thereby you can make use of this tool in future node.js projects also I already installed this package globally into my machine and then we have this popular Express package here we have the corresponding installation command and then after that we have body parser which act as a middleware to pass incoming request bodies and in order to work with the mongodb this Mongoose package is really helpful in order to install your packages you can make use of the terminal from vs code like this but when I work with node.js projects I often use an external command prompt like this so first of all to clear these previous commands use this command CLS first of all make sure that we are inside the node.js project directory now in order to hide this lengthy folder path you can make use of this command prompt then donut D this command only worked with command prompt not with Powershell now hit enter now let's install those four packages first for nordmon as I already mentioned this package is already installed into my machine if you have installed this package globally into your machine you don't have to install it in each of the node.js project I'm installing this package again because I want to save this package in the dependencies of the project and thereby when you download this project from the GitHub repository it will be automatically installed if someone is not installed by globally then after that we have Express body parser then Mongoose hit enter so here we have successfully installed those four packages you could see the same inside this package.json file now let's pause for a moment I would strongly recommend you to go through the video description where I will be posting comment error bugs with this solution and also let me know if you are stuck anywhere during this development so that I can provide quick fixes or I will come up with a better solution next time now inside the root index.js let's start this server for that we have to import the Express package then as usual we will be creating a variable called app into it will assign the value returned from this Express method now in order to start this server we just need to call this method listen as a first parameter we have to pass the port number Let It Be 3 000 and then we can pass a callback like this it will be invoked when the server is successfully started from the given port number so inside this method we'll print this server started at three thousand in order to verify this same let's run this application so here we have the command prompt from the same project directory while developing a node.js application I will place both of this IDE and command prompt in parallel so I will do this now let's floss this command prompt onto the white side now we could see both the source code and the server log in a single screen now inside the command prompt let's hide this path in order to run this application normally we do this not the name of the root file which is index.js since we have installed Norman we can use that here so that we don't have to restart the application after each change we make inside this folder or project directory here hit enter boom so here we have successfully started this server at the given port number now let's import the package body parser we'll use the package to convert the request body into a given format that will be done inside the middleware that you can call this use method so this is what we configure middleware so from this body parser we just need to call this method Json so whatever request comes into the application its body will be converted to the Json object now before going forward let's set up a database in mongodb in our YouTube channel Community I have conducted a port on regarding which approach would you like to choose to work with mongodb whether you like local mongodb server or the online mongodb Atlas around 60 percentage of force goes to online mongodb unless so I'll be using online mongodb Atlas for this project in order to participate in such question answer sessions please consider subscribing to this channel code of action then only you can participate in community post like this so first of all let's go to the website mongodb.com if you already have an account go for sign in now for those who are not familiar with this online mongodb server I'll be explaining how to create one for free now first of all click on try free here now the steps that I'm about to show you will be different in future because this interface of this website will be different from time to time but it will be fine if you follow these steps here now let's sign up by providing these form datas here or you could sign up with your Gmail account using this button here so I'll be using this Gmail account here now accept these terms and policies click on submit so here we have the welcome message just beneath that we have some questions to answer finally click on finish now select this free tag option click on create don't bother much about these Cloud providers and then the region near to you will be selected by default stick with default options if you are not an expert on these terms here finally click on create cluster so finally here we have created a mongodb cluster you could see the success message popped here now it is asking for for the credentials and Etc we can discuss that in a bit now go to this database so here we have the default cluster cluster zero now click on this cluster now here we have the collections tab this is where we store data from our application we'll do that in a bit before that we have to provide some initial configurations inside this left side menu here you could see network access click on it here we have to provide an IP address from which we access this online DB if you have a particular IP address you can provide that here for now I will just use this option here allow access from anywhere so it will be having this zero IP address now since I'm using this cluster for a demonstration purpose here I will check this temporary option so that this cluster will be deleted within one week you don't have to check this temporary option if you are looking for a permanent cluster finally click on confirm once it is done go for this database access here we need at least one user to connect with the DB let's create a new user username will be something like admin password one two three four now we need to assign a role for this user so I'll select that from this built-in rows click on ADD built-in rows I will select this Brawl for read and write operation finally click on add user now back to the database now let's try to connect with this cluster from our node.js application in order to show you the source code at maximum available zoom in level I will expand this vs code to the full screen then I will usually go for the Zen mode so that you can use the shortcut hold Ctrl then press K release both of them then press e z so here we are in Z mode with less distraction now to see the Explorer from the ID you can use the shortcut Ctrl B now inside this app structure we have this Javascript file DB inside that will be establishing the mongodb connection so let's create the file DB Dodges first of all let's import the package mongoose now inside this variable I will be saving the URI to the cluster that we have just created in order to get view all right click on connect then select this option connect with your application so here we have the corresponding URI copied the clipboard let's paste it over here inside a string like this so here we have the default cluster name cluster 0. here we have the one and only one username admin now here we have to provide the exact password one two three four now after this mongodb.net forward slash and before this question mark we have to provide a name for the DB inside this cluster we have not yet created a DB we only have the cluster cluster 0. inside this collection click on add my own data inside the API we'll be dealing with employee records so I will name this DBS employee underscore DB and the employer course will be saved under a collection with the name employees click on create so here we have the DB and under this collection employees will be adding employee records now inside this DB URI let's replace this with the corresponding DB name which is employee underscore DB now in order to establish this connection we can call this connect method as a first parameter we have to pass the URI now what I'm going to do is I'm going to export a function by default so here we have the anonymous function inside that I will call this method and we will be returning the promise that is returned from this connect and let's import the same inside this index.js let's separate the local Imports like this just to differentiate from the package Imports at the top so let's import the anonymous function as connect DB and we could import that like this and by default it is returning an anonymous function and we have assigned that to this connect DB now in order to establish the connection we have to call the method connect DB if it is successful we can handle that inside this callback here if there is any error we can cast that here since we are returning the promise from the DB file this road index file could easily extend the success and error callbacks as per our requirements let me explain once the DB connection is successful we have to print the message DB connection succeeded like this here we have already started this server even before connecting with the DB now it is better to start this server after establishing the DB connection since we are handling the promise inside this index.js we just need to move this listen method invocation into this success callback here hope you understood my intention here we are already running the server inside the terminal here inside that you could see this deprecated warning in order to hide that we have to add few more options while connecting with the DB here you could see the same inside this connect window that we had inside this pop-up window check this option here you could see these options for URL pass and unified topology we can't use the exact code here because they are using a different package so let's copy these options and let's pass that as a second parameter inside this connect method we only need these two properties so let's remove this last server API property save this back to the terminal even after that I'm getting this duplicated warning message in some of the old versions of the package we have to provide these options to hide the warning deprecated warning in this case we have a different deprecated warning and it is only uh shown in the latest version Mongoose 7 version so in order to hide this warning we just need to call this method to reset this strip query feature so I will do that here now let's check whether we have the same warning or not boom that's it so here we have successfully established a connection with the mongodb now let's create this employee controller here for that first of all we'll be creating a new folder called controllers inside that we can add this controller file employee DOT controller.js first of all let's import this Express package in order to make use of HTTP verbs get put post Etc we have to create this router object here by calling the method router now in order to make a get request we can call this get method like this so here we have the corresponding path which is a forward slash then here we have the function to handle the request with these two parameters request and response now in order to interact with this DB employee DB or the collection employees will be creating a corresponding model class as you can see here so first of all we need a folder called models within that we have the model employee now first of all we have to import the Mongoose package in order to create a model we can call this method model as a first parameter we can name this model like this employee then as a second parameter we can pass this schema which is the structure of the data that we are going to save into the DB collection so related to the employee entity we have these properties full name which is of the type string then we have position location and salary the corresponding type will be number so here we are passing the schema as second parameter now as a third parameter we can pass the name of the collection that we want to use for saving data related to the model which is referring to the existing collection here employees but in this case we don't have to explicitly mention the name of the collection by default this method will use the plural of this model as the collection name for the model here so the data related to this model will be saved to this existing collection employees now I will expose this model by default like this now back to the employee controller now with this get method we need to retrieve all of your course under the employees collection for that normally this is what we do first of all we'll import the model since we are inside the controllers folder use double period so that we can get outside this folder controllers then outside that we have this folder models inside that we have this model file employee.model.js from that we are importing this model which is exported by default now this is a common convention to name models using Pascal casing instead of this we have to use Pascal Casey meaning each of the word first character will be uppercased so this is the common convention now in order to retrieve all of the existing records we'll call this method find we can handle the success callback like this and if there is any error we can handle that inside this cache block if there is any error we will print that inside the console like this now let's handle the success response we have a single parameter data which is returned from this uh method fine so it will be an array of records from this employee's collection so we just need to do this this response dot send method can be called into that we can directly pass the data so here we have a GitHub method to retrieve all of the existing records from the employees collection now let's check whether it works or not for that first of all we have to export this router object here and then import the same inside this index.js employee routes and let's import the corresponding controller file now with this employee routes we can configure routing for the application by calling this app.use method first of all we have to provide the base path it will be forward slash API forward slash employees and then we can pass the employees routes so basically this application is hosted on the Local Host server within this port number 3000 so in order to make a get request to this get web method here we could do this localhost 3000 then we have API forward slash employees sorry employees API forward slash employees and then inside this controller we have this forward slash let's append the same at the end here hit enter boom that's it so here we have the empty array returned from The Collection there is no record inserted into this collection employees so the get method is doing its job now let's look how to insert new employee records into this app employees collection here for that we can call this method post path will be same and here we have the function to handle the request now the object or employee object that we want to save will be passed within this request body like this and it will be converted to a Json object because of this middleware here so let's look how to insert the same into the mongodb collection for that we just need to call this method create from the model as a first parameter we have to pass the object which is so inside this request body property and we'll hand the success and error call back like this here so let me copy paste that here now let's check how this post web API method Works in order to test or verify web methods other than get we need an external software like this Postman here you can easily download the software from this website postman.com through which we can make any type of HTTP request so here is the corresponding software interface let's make a post request so select post from this drop down the URL of this post web BPI method is same as that of this get so I will copy that from here paste that inside this URL session now switch to this body tab here and then select raw select this Json format so here we have to pass a Json object containing the details of the employee that we want to save inside this mongodb collection for that first of all I will copy the corresponding model from this model class here and let's paste the same we have to wrap these property names inside the Json with chords like this so select the property then type the calls double quotes and let's provide the corresponding details here so here we have the new employee era Elliott now let's make this post request for that you can click on the send button boom that's it so here we have the return object from this success callback here now let's check whether it is inserted into the corresponding DB here refresh the page so here we have the properties that we have passed and here we have the ID underscore ID to uniquely identify the course from The Collection the same object is returned inside the response now let's make this get request again refresh this page so here we have the array containing the inserted object you could make the same get request from this Postman create a new tab the space URL click on send so here we have the array containing the newly inserted employee object now you see here we have the status 200 meaning okay everything that we meant with this uh web method is successfully executed you could see the same inside this post response also now as per the common convention instead of returning a status 200 while creating a new record we have to return the status two naught one indicating a new record is created so instead of returning this object as it is I will do this status and we will pass 201 indicating the record is created inside the web API now we can return this data inside this method Json like this in order to verify this same let's try to insert another employee so I will modify this request body here click on send boom that's it so here we have the status called 201 indicating a new record is inserted and here we have the corresponding employee Daniel hacker let's verify that inside this mongodb collection so here it is now I would like to apply some best practices into the application before that it would be better to explain those best practices if we Implement one more get verb method to retain a specific employee with the given ID so let's copy paste this get web method this time we have a query parameter ID and in order to retrieve a specific employee with the given ID instead of this fine method we can call this method find by ID now the ID that we have passed through the URL can be accessed with this request parameter so here we go request dot patterns.id now since there is a possibility of not having error code with the given ID we have to handle that inside this success callback here so let's expand this success callback if there is no record with the given ID then this data returned from this find by ID will be none so I will do this will only retain the data if data is present meaning data is not null if it is null first of all we have to set the status code AS 404 meaning not found and then we'll be passing a Json object like this error is equal to no record with the given ID let's concat this ID here that's it now first of all I will make this get request to retrieve all of the existing records now we are going to make a get request to this route here to retain a specific record with the given ID for that we just need to pass this ID after this forward slash like this let's make this request boom that's it so this time this get method with the ID is consumed since there is a record with this given ID it is written inside this response now let's change this ID inside the URL and let's send this request again we are not getting any response if you check the command prompt you could see the exact reason see cast to object ID is failed this underscore ID which is the primary key of this records inside the collection can't be any string it should be following the criteria mentioned inside this uh detailed error message so these are the requirements to be a valid ID or object ID of the record so first of all we have to check whether the past ID is a valid object ID now in order to validate the ID pass to this get web method first of all I will import this object ID from Mongoose so here we go object ID first of all we have the impulse statement for the library Mongoose inside that we have types namespace inside that we have the class object ID now we can call the method is valid from this object ID class like this is valid inside that we just need to pass the ID like this so if it is not valid we can return the response like this first of all we'll set the status 400 meaning bad request this State score will be returned from the server API when we pass invalid data in a request and we'll pass edges on object with the error message given object ID is not valid we will only retrieve your corresponding record if the given ID is valid now let's check whether it solves the problem the server is already running so back to the postman let's try to make the request again with the new changes boom so here we have this it is called 400 given object ID is not valid the proper ID is this and we got the corresponding record already now let's try an ID which is valid according to mongodb object ID restrictions that's it we got this it is four not four not found meaning the record that we are looking for is not the inside the collection so first of all it will check the ID that we have given if it is not valid we will get the bad request response with this error message if there is an employee with the given ID we get the corresponding employee details if there is no employee with the given ID you will get this 404 not found response so far we have three web methods now before implementing the rest of the delete and update operation let's apply some best practices to organize this controller here now first of all I want to Define an object with generic methods which implements great operations for a given model so first of all I will Define the object with generic functions and then later I will explain how it works so as per this app structure we need one more folder called services so I will create the folder here called services and inside that we will have this file index.js inside that I will Define this function generate credit methods for this method we have a single parameter model as I already mentioned it's a common convention to use Pascal casing for models so I will use the Pascal casing now inside this method we are returning this object so first of all we have this method get all for this method we don't have any parameter now inside this method we just need to return all of the records for the given model here for that we can call the find method from the given model this will return a promise so return the same promise back to the caller from this get all method since there is only one statement inside this method we can even simplify this method like this now let me duplicate this so here we have get by ID method for this method we need a single parameter ID and here we will be calling this method find by ID into it will pass the ID now let's duplicate this for the insert operation so I will name this method as create corresponding parameter will be record object which is to be inserted into the model so we have to call this method create and let's pass this parameter record here for now this is enough we have to add two more methods that we can do in a bit now along with defining this function here let's export the same so you could do this exports dot generate credit methods now we have defined the generic method here let's use the same inside this employee controller so here we go from the service class forward slash index.js since we are importing the method from index.js we don't have to mention that here it will work even if it is not here it will be assumed by default now from that we have to import the method generate credit methods so here we go employee cred it will call this method into that we can pass this employee model so this generate method will return this object having these methods defined here and these models will be replaced with the model employee model that we have passed now in rest of these web methods in order to retrieve all of your codes we could do this employee credit dot get all method can be called since it is returning the promise from the corresponding Mongoose method we can use the same success and error callback like this now in order to retrieve a single record with the given ID we can call this method Employee Credit dot get by ID in case of this post operation we can do this employee credit dot create method can be caught so in this way these generic methods could be reused in different controllers with their respective models so let's go for the next change now in order to implement rest of the operations like I update and delete we have to define the routes like this router dot put can be called for update operation and here we have the corresponding path inside the path we have to pass the ID of your code which is to be updated and then here we have the corresponding hand love function now for the delete operation we just need to replace this put method with Delete now invoke method with path containing the ID we have to validate the ID like we have done inside this get method here so let's find a common way of validating VDB ID for that first of all we have to understand how to handle multiple request Handler in a single web method for example consider this get web method so here we have the request Handler function with two parameters request and response so this is the Callback function in order to handle this request here like this we can have any number of callbacks so let's add one more callback here first of all this callback will be executed then it is called back if there is anything after that then that will be executed so the order will be in the same order that we have provided here now let's try to make a get request into this new route so employees forward slash test hit enter see the rugers is getting processed non-stop because we have not returned anything as a response inside these callbacks in order to solve the problem I will return a string from this first callback now I have placed both the IDE and Postman side by side so that we can see both of them in a single screen now let's make the same request again that's it we got the response from the first callback here now let me move this response to the second callback let's make the request again see here we have the same problem the request is not ending because from the first callback we are not returning any response so the request is trapped inside the first callback in order to solve this problem we can add a third parameter to this request Handler which is next so with this parameter next when a request is having multiple callbacks like this we can hand over the execution of the request to the next callback in the order so in order to pass the execution to the next callback in this order you just need to call this next here that's it and let's make the request again boom that's it so in case of a web method having multiple callbacks like this with this next parameter we can transfer the request to the next callbacks there are few other benefits of using this next method we will discuss that in a bit so let's do the same for this get method here I'm going to Define as a prayer request Handler in order to validate the ID and then I will paste that just before this request Handler here so I will do that inside this folder middlewares okay middle ways inside that let's create this file so here we have the function validate DB ID having these parameters requests response then the next method let's copy paste the same from the controller in order to switch from files inside this project instead of this Explorer here you can use this shortcut hold Ctrl then press P then just type the name of the file like this okay let me cut this to switch between open tabs inside the IDE you could use this shortcut hold Ctrl then press tab so here we have the index from the middleware and let's paste it over here and let's copy this object ID in post statement so if the validation of the ID phase will respond with 400 status code meaning bad request and will provide this error message else we'll call this next method meaning the DB ID is valid so that we can go for the next request hello or callback in the same web method now instead of default export I will use named export meaning we are exporting an object like this and we'll export this method validate ID now back to the employee controller let's import this same here from middle ways we don't have to provide the name of the file because by default it will search for the index file and let's import the same here validate DB ID now we just need to provide this request Handler just before this callback here let's remove this else part that's it see first of all the request will reach up to this request Handler and from there it will validate the ID if it is not valid it will return f 400 bad requests if it is valid then the request will be hand over to this second callback here now we can reuse the same request Handler for any web method having ADP ID not only just for employee controller it is a common method so I will do the same for this update and delete operation we have not yet implemented its corresponding request Handler here we will do that in a bit now like this request Handler I'm gonna Define one more request Handler to respond with 404 not found error when the ID is not present because it can be reused in other web methods having the ID like this so I will name the function as race record not found error in order to simplify the name instead of not found I will use the corresponding status code for not four so here we have the function with these parameters request response we don't need an X parameter here because we are only returning a 4 node for response once so let's copy that from here so we have responded with four node for error with the corresponding error message inside the error message we have also appended the corresponding ID so let's do that for this error message also for that I will use the template string now let's export the same here and we can import that inside this controller and we could pass that here into that we have to pass these requests and response parameters so we can make use of this method when the given ID is not found in the collection now before implementing the final update and delete operation let's define a common error Handler instead of printing the error message as it is inside the sketch block and also if there is any unexpected error the common error Handler will be helpful before defining the command error hello back to this test get web method here we have seen the application of this next method when a web method is having multiple callbacks just to transfer the request to the next callback like this now into this function next if we pass an object like this then it will assume that there is an error during the execution of the request so let's check that inside this Postman previously we got the response because this next transfer the execution to the next callback here this time we have an object passed to the function so let's check what's the output here if hit enter see here we have the response with this state is called 500 meaning some internal error has occurred inside the server so whenever we pass an object to a next function not only an empty object any object the request will be transferred to a common error Handler if there is one now in order to handle such error response we can define a common error Handler that we can do inside this middleware index file so here we go error Handler normally a request Handler will have these parameters request response and if you need to transfer the request to other functions we'll have this third parameter next but in order to handle another response we need a force parameter so here we have the fourth parameter of error the error object will be passed to the force parameter so here we have the function body first of all we will set the status as 500 meaning some error has occurred inside the API and then we can pass the error object here error is equal to error in short you just need to do this now let's export this same here now we are going to import this error Handler into this root Javascript file index.js as a middleware in order to define a middleware we have to make use of this app.use method right then only we can catch all the error responses from the application at this road level so here we have the corresponding import statement now let's pass that here to this use method error Handler now make sure that you are passing this error Handler after adding rest of your routes inside the application it can only catch error responses from routes added prior to it okay now back to the controller now let me get rid of this test web method now instead of printing the error message here we just need to call the next method and into that we can pass this error object for that first of all we have to add the next parameter here now let's do the same for rest of the web methods let me copy this and pasting here same for this post web method also so that's all about the fine tuning now everything is properly organized now let's go ahead with this update operation first of all we have already validated the ID pass to the request now let's go ahead with the update operation for that first of all inside this crap methods I'm going to add a method for update operation so here we go update for this method we have two parameters ID and the object containing the updated value so here we just need to call this method model dot find by ID and update as a first parameter we have to pass the ID then the record containing the updated values and finally we have to pass this object with options there are a lot of options to configure but for now we just need to set this property new as true by default after the update operation this Mongoose method will detain the corresponding record object with previous values instead of that inside that object we need new values after the update operation that's why we set the new option as true here now before going back to the controller let me add one more method for delete which is having a single parameter ID which is the ID of your code which is to be deleted so here we go model dot find by ID and delete and let's pass the ID here so inside this object we are returning all the methods required for accurate operation now basically with a service folder like this we'll be having separate service courses for each model so in a real world project there will be a separate file for employee like this employee service.js now inside this index file we have handle all the basic red operations now if you need any other query method related to employee we can add that here and then you can make use of that inside this corresponding employee controller now let's implement the update operation here so the newly added method update will be the inside this to an object you just need to call that here employee cred dot update and let's pass the ID from the request and the new values of the record will be the inside the body prequest body now let me copy paste these callbacks here now inside both of these update and delete operation before the actual operation it will look for the record with the given ID if it finds such a record it will go for the update or delete operation so inside this success callback if there is an object inside this return parameter then we can say the operation is successful so in case of this update operation it will be an object containing the new values of the record if data is null meaning there is no record with the given ID in that case we can erase the 404 error and in these web methods when we return a response like this using the send method the corresponding response will be having 200 status code also called as okay response or success response meaning the operation that we intended with the web method is successfully completed we are returning a different status code in case of the sports web method because we are creating a new record in those cases the best practice would be to return a two naught one response meaning the operation is successful and also we are giving an extra information that we have created the object and we could keep this error called back as it is so here we have the get method to return all of the employee records if you want to have a better formatting for this returned Json object here we can use this Chrome extension Json format currently I have enabled the extension that's why you could see this interface if you select the past portion the same Json object is shown with proper indentation and thereby it's far better for readability now let's give a rise for this employee ever Elliott so I will copy the ID now back to postman let's make a put request so select put from this drop down let's copy paste the URL from this previous post request and let's paste the ID now switch to the body tab here select draw then select Json from this drop down now we just need to pass an object containing the updated value now we just need to update the salary of this employee so I will do this send the request boom that's it here we have the status code 200 meaning everything when successful and here we have the updated employee details you could check that inside the DB also so here we have the updated salary for the employee ever I forgot to mention about this extension here in all of my tutorials I used to enable this extension dark reader in order to have a dark mode in all of the websites just to avoid the eye strains from device screen now let's implement the final delete operation for that we have already defined the function inside the describe methods delete so here we go employee cred dot delete method can be called let's pass the corresponding ID from the request now let me copy paste this success and error response keep this same as it is if the operation is successful the corresponding deleted object will be returned if there is no record for the given ID then F4 Note 4 response will be written now let's try to delete this employee Daniel let's open a new terminal select delete so here we have the base URL and finally just append the ID of this record send the request we have successfully deleted the employee let's make this get request again let's refresh the collection so that's it guys here we have implemented the credit operations insert update delete and retrieve in the node.js stressful API with mongodb you could download or clone this project from the GitHub repository link given in video description if you found this video helpful please consider subscribing to this channel called affection please like and share this video with your friends and colleagues have a nice day bye
Info
Channel: CodAffection
Views: 19,831
Rating: undefined out of 5
Keywords: node.js crud operations, mongodb crud operations, node.js restful api crud with mongodb, node.js api - insert, node.js express mongodb crud, CodAffection, node.js api beginners tutorial, moongose crud operation in node.js api, Connect MongoDB with Node.js, Restful API CRUD with Node.js API, Express & MongoDB
Id: 00nHyY-r5e0
Channel Id: undefined
Length: 47min 51sec (2871 seconds)
Published: Wed Dec 21 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.