Build A Social Media Backend REST API With Node.JS | Node Express MongoDB Real-World Project |

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] thank you [Music] about the mongodb it is like a nosql database which stores documents in the collections instead of the record as tables as in the SQL database so if you are coming from the SQL background so you can refer the record as the document and the collection as it tables so what is the node.js so it was a JavaScript engine originally built for the browser which we known as as the Chrome's via tinjan and now it can work without a browser as well and having some extra apis that we can use and it allow us to run the JavaScript outside of the browser so it was the Chrome's weird engine which is now maintained as a standalone project which we call as the node.js and having some extra apis that we can use and then what is the express.js so it is like a framework for the node.js which help us to ease the process in handling the server-side tasks and then relations between the collections so we have the relations between the collections inside the mongodb so we have two collections here with the user and with the blocks so each user can have multiple blocks or we can say each user I can have the array of the blocks but each block Belongs to Only single user inside this application so this is the structure of this database that we are using so this is the schema that we are using from the database I have opened a folder in the vs code and now I will create a new folder here as the backend so or both applications will be there in the folders as the backend and the front end so I have created a backend folder and now we need to go to the terminal select a new terminal from there and then we need to move on to the back end so now we are in the backend directory and then we can install the node.js application here so the command to install the node.js application will be the npm in it so for this command now it will ask you some questions like the package name of our application like it can be a backend the version can be so initially can be the 1.0.0 as the same the description you can add as a Blog application and then there is some entry point so we can change the entry point to the app.js As for the structure of our application and then we have the test command so we can just skip all of these things like the test command git repository keywords author and the license and now you will see all of the details will be now shown there and it will ask you for the one last time as is this okay and once you will click the yes then you will see the package.json file will be created and it will contain all of the details of our application like we have given the name as the back end we have given the version as the 1.0.0 we have given the description as the blog application and then we have the main as the app.js and now the package.json file is created and now we can install some dependencies to use the application so first dependency I will install is the node mod so the node one will help us to detect the file changes in our application so so if any of the file will be modified while running the application so it will restart the application once again with that overwritten file so we need to install the node bond with that and I will show you the demo with the note mode like how it works so now the node one is installed and now we can see the demo so we can just go to the scripts and then we can just add a script at the start so this will be a start script and then we need to add here the node mon which we have just installed and then the app.js so the nodebon app.js will then listen for the file changes of the app.js file and all of its code inside that and even if we have returned functions which are available in the other file so it will still detect the file changes and it will just rerun the app.js once again and then I have written the start command as this and then we can just create a new app.js file in the back end in the root directory so app.js file and then we can just add just a console log here to just check if everything works fine or not so it will be the hello world so if I will now save and to run this application now as we have written the command as this start so we need to run the command as the npm start there and then you will see the node one is now running and then it is showing us the hello world and now about which I told you about the file changes like if you will just change anything like the hello world you can just set three as collaboration marks there and then once you will save the file now you will see and the node one will just rerun the application for you and then you will see the automatically the file changes will be detected and all of your code will be updated instantly so now we have installed the node one and now the next thing we need to install is Express so we need to install the express.js as we have discussed in the introduction video so the expressjs is useful for the node.js applications to handle the HTTP task and for running the HTTP servers so we need to install the express as the npm install as the express so from this command the express.js will be then installed and then we will just create a basic application with the react with the express.js so the express.js is now installed and to verify the installation you can move on to the package.json file anytime and then you will see there will be a dependencies array dependencies object and then you will see there will be the express and then we have installed the node one also so now the express is installed and now we can just use here the express so now for using the es6 import and the export modules in the node.js now we need to do one thing we need to add here the type after the main so we can add a type and it will be the modules so we need to give our type as the module and then in the start now we have to change the start script here so the start will be then there so you can just copy that so it will the node one and then it will be the dash dash as the experimental Dash modules and then you have to add another two dashes as the es Dash module a specifier and then again one dash it will be the resolution and it will be equals to the node so this will be the updated start command for now so I will now just minimize this now you will see this is the start command as the node mon as the experimental modules and then you will see there's the uh there with the Es at the module specifier which is resolution equals to the node and then the file name will be close to the app.js so this will help us to Import and Export for the es6 so this will help us to use the ESX import and the export modules in the node.js as well so we need to go to the app.js and then we have already installed the express so we can just import the express from the express build Express from the express one of the experiences there and then once you will hover over it it will show you the namespace of the express so now the express is there and then we can just use the Express in the app so we will declare a variable as the app and the express.js will give all of its functionality to the app variable so it will Express so now the express has given all of the reference to this app and then we can just create a bit like it with Zig application like we need to create the middlewares and the middlewares are used for handling some tasks in the application so to the app dot use so this will be the middleware and then it will be the URL there and then the URL can be the main URL which will be there and then we will add a call by function after that which will be the request response and the next so this will handle the URL of the default URL like if I will not move on to the localhost at the 5000 so it will be still there but if I will know where there is the API or something so it will only work for the API so we will stick with the API for now and then you will see request response and the next and then we will just send the response to the server like response dot send I came to the hello world and then this will be the application and then we need to use the port as well so we need to Define here the port in which the application will run so it will the app Dot listen so it will just listen to the port of the 5000 from this function so we need to Define here the port like the 5000 so now if I would now save now it will restart the server and now if we can move on to the localhost at the red 5000 so it will be the Local Host it will be the 5000 and slash API but then you will see it will shows the hello world down to the web page because now we are sending the response as a hello world to the server so this is something which we needed so this is a basic express.js application with the node.js which we have just created and now we need to move on to the main application which we want to build which is a Blog application and this structure we have already defined the structure in the demo application that what will be the structure so I will recall it for you so there will be the application there will be some routes for the user and for the blogs as well so first the user will sign up or login and then he or she will be able to see all of the blocks which are available from all of the users and then there will be an option to add the block for the user for the specific user which I have just logged in and then he or she can just create a new block for the food and then he or she can delete or just edit or update that record since the application which you want to win so there will be two tables or like two Collections and then we will get a reference between the two collections to create this application so let's start building the application so I will now remove this app.user and then now we need to move on to the mongodbit to create the database and for creating the database we need to move on to the mongodb website and then we need to just build a new project from there so now you can see there is a sign up page so we so we need to just click on the sign in from there and then there will be the option for the login with Google so you can try out with the Google or if you have your email and the password so you can do with that so I will not try with the Google and now you will see now we will create database option so now we need to just move on to the drop down and then we need to click on the new project so we will create a new project from the scratch in the mongodb and then the name of the project can be the block app and then we can just click on the next and then there will be some options so we can just skip that and we can just click on the create project button from there and then we can just move on and we can just build the database from there and then you will see there is a builder database option so we can just click on that button and then you will see there will be the mongodb atlas so you can just select according to your preference if you want the serverless then you can go with that serverless and it will cost you around 0.30 dollar per 1 million reads and then if you want to go with the dedicated then you can just click on that but I will now create a free cluster which is a shared so we can just create that with the free so I will create a free cluster and then you can see there will be some default options so I will now move on with the default options as well so I will now click on the create cluster from there and then it will prompt you for the username or password so you can just create a username here like we can create the username as the admin and then in the password we can just Auto generate the secure password from the mongodb and make sure you copy this password and then you can just store it anywhere like this and then you can just click on the create user after that and then you will see there is a success message and then you can see now we have some ports and we need to just allow it from anywhere so we can just skip that but I think so we can just move on to the network access from there and then we need to select the IP from there so we need to click on the add IP address and then we can just click allow access from anywhere from there and then we can just confirm that entry and we have already set up the mongodb for that so you can say we have a database so it will be deployed in around one to three minutes so till then we can just move on to the app.js and we can just import the Mongoose to connect to the database so it will be the import as a mongoose and then we can use the mongoose dot connect function to connect to the database and for the URL now it needs a URL in the string and then we need to go to the database deployments and now our database is deployed so we can just click on the connect and then there will be the option of the connected application so you can just click that and then you will see the node.js is already there so we can just copy that and we can just move on to the mongos.connect once again and I will now copy the string so now you will see this is the string but now the ad the user is the admin which we have already defined but the password Here we need to just remove this password because this is not the password of our mongodb database so we need to just copy this password which we have just copied and we need to paste it here after the call on and then after this there is a my first database so this will be the database name in our mongodb so we need to change this it with the block so this will be the database name and the Mongoose dot connect returns a promise so we need to just add a then statement after there so it will be the then and then after that we can just console.log or we can just use the app Dot listen after that so we need to just get this and then I will now just add the app Dot listen to the port here because when the mongodb will be connected and then only we will just listen to the server of the 5000 and then we can use another then statement after that and then it will be just a console Dot Lock so we need to just add a console.log that's the connected to database and listening to localhost Port of the 5000 that we have just defined so this will be the console message which we want to show and this is a promise and this is a database request so it is type of a server request and it can fail also so we need to add here the cache statement also because if we don't add the cache statement then it will not catch any error if we will just get the error from the server so we can just add here the cache statement after that and then it will catch an error and then we will just console.lock the error just for now so it will be console.log the error so if I will now save and if I will now just check if everything would work fine or not I think something is missing there I think it is showing as as the module not found so I think there is an error so the error is that the Bongos is not installed so we can just install the Mongoose Library so it will the npm is the mongoose so the Mongoose will be then installed and then we will use the mongos from there and now the mongos is installed and now we can just check again with the npm start command if it connects to the database or not so we can just see the log if it connects to the database so I think so now you will see it is connected to the database and it is listening to the localhost at the rate Port of the 5000 and now everything till the connections of the mongodb is working fine and now we need to create the folder structure so there first there will be the backend and then there will be the folder of the routes so it will contain the routes and then there will be the folder of the controllers so now we have the routes and the controllers and then we will have one more folder of the models so the models will contain the models of the user and the blocks and the controllers will contain the controller functions which will control the API routings in the mongodb and the express.js and then the routes will contain the routing operations so from which route and from what route the controller will call so all of these things will be then connected and now we can just start creating the application and now the first thing which we need is we need to build the model and the model of the user so user model will contain a schema in which the user will be having the name field the email field the password field and the blocks field so we need to build the schema of the user first so it will be the user dot JS and here we need to import the Mongoose so it will be import mongoose from the Mongoose and now we need to import the schema of the Mongoose so we need to build the schema so we need to get the schema class of the Mongoose at this schema will be equals to the mongoose dot schema and with this schema then we can win the model of the Mongoose with the user collection so it will make a user collection for us so we can just make the fields like the schema of the user will contain the cons user schema will be equals to the new schema that we have just imported from the Mongoose dot schema and then we need to edit the object and then we need to add some Fields like it will contain the name field and the name field will contain an object and the type of the name will contain a string so we need to add the type in an object and then this field will be the required so we need to add the required path and the required part will be close to the two and if anyone with the request they won't provide the name now into the user schema then the application will break and then we can just provide here the email also and then email will be type of this as the string as the same as the user and then it will be the required field so the required will be equals to the two and then and then there will be the one more field in the email because the email should be unique of every user so we need to add here the unique as it true so the unique property will allow us to add some unique email also on to the users and then we can build the password field as the password and the password will be there as a type again as the string and then it will contain the required field it will also be the required will be equals to the true always and then we need to add here the minimum length so the password will contain a minimum lens so we can just add the minimum length as the six so now we have created the user schema so this will be the collection and all of these fields will be now stored in inside the collection so now we have just built the schema of the collection and then we can then we can just export The Collection into the mongodb so for exporting we need to export the collection so we can just export as the export as will be the default so it will be the default export and it will be the Mongoose dot model so it will create the model for us like a collection and then we need to define the name of the collection like it will be the user and then we can just add the schema of the user like we have the user schema so now we are exporting the collection and then in the mongodb the collection will be stored as the users so there is a Nami convection and by default naming convention in the mongodb that if you will store a name as the user the first letter is a capital letter then it will be stored at the small letter and all of the fields will be now in the small letter and it will be in the plural form like we have given the user as a singular form but in the mongodb it will be stored as the users so now we have created the user and now we need to just build some routes and the controller functions for the user so we need to move on to the routes now and where then we need to create a new route as a user Dash routes dot JS and now the user will contain some routes and for giving some routes now we need to import the I think the express so it will import the express from the express Js it will be from Express so now we have imported the express and now we need to use the router from the express so we can just create a variable as the const router will be equals to the express that we have dot router so we have a router inside the express and then we can use the router function from there and then it will provide its reference to this router variable and then we can just use all of the router Fields like all of the router requests like it will be having the router dot post router.get so all of the requests will be done there as they put also and for the delete also so all of the requests that we need contains here so now the first we need to just create a simple command like the get and it will be of the same URL that we have it will be the blank URL and then and then we can Define here a function and the function we need to define the function in the controller functions so then we can just Define here only a singular route like the same route and then there will be a controller function and for creating the controller function now we can move on to the controller folder create a new file as user Dash controller dot JS and then we can just import here the model of the user to create the controller so it will import the user from the model slash user so now the first route will contain a blank URL now we will get all of the users from the database so for doing that we can just create a function as the const as we get all users will be equals to an async function because it is an asynchronous task and HTTP requests are always asynchronous task so we have to use the async and then it will contain the response request response and the next as the parameters so the request is what we will request from the front end like the request.body request dot headers and so many things and the response which we will provide to the intent and then there will be a next function which will allow us to move on to the next available middleware and then we can just provide here the users all of the users to this route so we can just Define a let as the users and it will be by default undefined and then we have to use the try catch block because it is always because we have to use the try cache block always whenever we are dealing with the database because the database operation can fail as well so we should always use a try catch block and if we get an error so we can just log an error so then we can use the tricaster block and inside the try we can just provide the users will be equals to the await because it is an asynchronous task so away it will be there and then we need to add the user so now we have imported the user from there and it will be the user dot find so we have a function in the Mongoose with the find so it will find all of the documents with there so if you will just hover over the find you can see it will return an array like you can see it will return and query array or any other array like you can see it will get a list of the documents that matches the filter but now we are not defining any filter so it will match all of the records so then we are using the try catch block and then we can use here the validations also like we can use the if statement like if we don't have any users or the users is a falsy value then we can just return the response dot status of the request will be the 404 that no users found and it will contain the Json message and inside the message we can provide the message as the no users found so if we don't find any users initially also because whenever we will try this request so it will give us the no users found because we don't have any users available right now in the database and then if everything is clear then we can just return the response dot status will be equals to the 200 which is Success which can be okay and then we can just send the Json message again and the Json message now will contain the users so it will contain the users so we can just provide only the name only because it will act same as the users property and the value of the users so because es6 so it will provide us the same result so we can just save and then we need to just export this as well so it will be the export so now this function will be exported and now we need to use this function inside the controllers so in the by default same request then we can use the get users get all user from there then you can see the guitar user is now imported from the controllers and then we can use that and now everything is fine but now in the main app file we have to use the middleware to use the routes because here we are not giving the application an idea that we are using the routes so we need to provide that idea to there like we need to import here the routes file so it will import the route I think the router is there from the and I think we haven't exported the router from there so we need to export the router as well so it will do the export so to a default export as the export default as a router so we are now exporting the router from there and then we can just use it the router as the app dot use and now it will auto detect the router if we will add here like the router now you can see it will auto detect that and then it has imported from there as well and then we are using the router and then if you want to use a specific URL from there so you can just add a specific URL like it will be slash API slash user so now every request will work as the HTTP Port of the 5000 and then it will work as the Slash API slash user and then all of the routes in the user will work so it will act as this and then if we are giving only the same URL so it will work as the same but when we will provide the sign up or we login so it will actually sign up until login and this route will be accessible from this URL only from API user so we can just provide that API in the user and then we can just remove that so if I would now save and if I would now move on to the postman which we have just installed and this is a screen of the postman then we can just create a new request from there it will be HTTP slash localhost Port of the 5000 that we have defined and then it will be API slash user and then it will be the same route so if I will not just create a get request now you will see the users is giving us an array because now we are having the users in the database and it is giving us an array of the blank array because now the users is a successful but we don't have any user and now we can just create the user by creating a sign up route so we can just move on to the user routes to create a user so it will be the router and it will the post function because we will just send some data with the request so it will be dos slash it will be the ad or it can be the sign up so it will be a sign up request a post HTTP request and then we can just Define a controller function there after that so to the export as the const and it will be the sign up function and it will be equals to again the async and request response and the next inside the function parameters and then we can just Define here this sign up functionality inside this controller function and for defining that now we have to get something from the request.body because the request.body contains something which we will send from the front end so now the postman will act as a front end and then we will send the name the email and the password from the postman so we need to get all of the things from the request dot body and the request.body is like it will contain the body now you can see there we have a body Tab and this will be acted as the response.body so we can just get the requested body so it will be const so we can just restructure all of these fields which we will receive from the request.body like the name we will receive the name field the email and the password and it will be equals to the request dot body so we can just restructure all of the things and then now we have to Define here something because now if we are giving a sign up request so now if the user is already available in the database then we have to add here some validation and for adding the validation now we can Define that existing user and it will be equals to the by default it will be then defined and then we can just again add a try catch block as we have earlier discussed that we should use the try catch block always but will the catch so whenever we will get an error we will just console.lock the error as of now and then in the try then we can just Define as the existing user will be equals to the await because it is an asynchronous task and now we need to find the user so if the user is already available then we can find with that like the user dot find and then we can use the find as well but there is a one more function as the find one which will just find of only one record from the database and for the filter we can just Define the filter as the email so we will just filter the email query and email is available in the user model as well so it will just filter the email property and the value of the email property which we will just send from there from the request.body so the email will be accessible as the key value pair there and then it will just fetch one record so if the record is there we can just add a semicolon there so if the email is there then we can just add here some validation like if the existing user is already available in the database and then we can just return the response I can do the response dot status and the status will be equals to the 400 which is unauthorized and then we can just add a Json message also like the message inside that key and then it will be the user already exist and then we can just add here the login instead so this will be the message which we will send if the user is already available and then if the existing user is not found then we can just create a new user so to create the new user now we can just create the user as the user will be equals to a new user it will be the new user document so we can just refer the collection of the user the model of the user and it will contain the object and it will contain the name of the user that we will Define it will be the email which we will Define and then it will the password so we are now defining here without the key value pair as in the ESX always and then now we can Define the user so and then now we need to save the user as well and to save the user now we need to use the try catch again because it is a HTTP task so we show you the try catch block because it is a database task and then if we will get an error we will just console.lock the error again and then to save the user we have a function as the user that we have just created which is the reference of the new document is it will be document dot save so we have a save function inside the Mongoose which will help us to save the document inside the database so this will help us as the user.safe and then we can just add here the return statement after that so it will the return response dot status will be the 201 which is created and then we can just add a Json message again and this time the Json message will not contain a string or anything it will contain the main user which we have just defined and which we have just saved and then it is a asynchronous task so we should add here the await as well because it will be user.save and it can take some time as well so we can just create that and now if we can see the sign up request what we have created in the controller function we have created the request.body fields which we will receive from the front end we have defined the existing user we are now checking the validation if it is already available and then if available then we are sending the 400 response and if it is not available then we are sending a new user and with the try cash block we are saving the user and then we are returning the status of the 201 and sending the user as the Json and then we need to use the other routes again and it will the sign up function from there so it will the sign up so we have imported the sign up as well from the user's controller so if I will now save and if I will now move on to the post when create a new request from there so I can now copy this URL from there and paste this URL here total the user slash sign up because this is a sign up request and we have defined the sign up and then this is the post request so we can just get here the post request from there from this drop down and now we need to send the body to send the body we need to move on to the body Tab and then we need to select the raw body which will be of type of the Json so you need to click on the text and select the Json and now we need to add here the Json so Json will contain the name field as we have defined and it will contain the name like I will provide my name as of now as the nickel and it will contain the email so we can just provide the email name as the nickel at the redtest.com and then this will be the email and then we can define a password can be like one one one one one one one as of now and if I would now save now we will see a interesting thing if I will now just send a request now you can see it will just not send any request now you can see it will throw us an error in the console so if you will just see that error that what is the error then you can see cannot destructure the property the name of the request.body and it is undefined and now this error is coming because this application Server doesn't know that what type of data it is receiving from the request.body so we need to tell the application that we are receiving the Json body so we can just use a another middleware before every middleware so it will be the app dot use it will be the express that we have imported in the app dot Json so it will just consider it will just pass all of the data into the Json format and earlier we were using the body parser but now the express has a solution so we can use the solution from the express only so it will be express.json it will get the Json data from there and now you can see the request for still sending so we can just go to the user's controller because the request was still sending from there and then we need to just stop the request so if there is an error then we can just return there like the console.lock only and then we can just return here a new error from from by creating a new error class or we can just return a notification from there but now we are now only returning the console.log so to the return statement as well from the catch so if I will now save and if I will now just move on to the postman ahead and if I will now just create a new post request again and then you will see the status is the 201 and then we have created the user and it is sending us the user in the Json so now everything is working fine but here we are receiving the two fields which is the ID and the V and this is coming from the mongodb because we have not stored the document in the mongodb and in the collection of the user and then we are now retrieved and now we are saving the password of the user as the same which we are receiving inside the mongodb database so we can just secure that also so if you want to secure it then you can just create a hashed password to store in the mongodb and with that the password will be encrypted so you can just use the encrypted password and to use that you can just stop this server and then you can just install a new library as the npm install as the Big Crypt JS so the big crypt.js will then allow you to just create a hashed password to store inside the mongodb so you can just install that Library and then now the Big Crypt is now installed and now we can just move on to the user's controller import the Big Crypt from the decrypt.js and now the big rate is imported and now when we are signing up the user then we will just create a password like it will be the cost at the hashed password and it will be equals to which we have just imported as a decrypt it will be the hash sync so till the hash sync it will just Hash a password in the synchronous way so we can just pass here this the string inside that and it will contain the password inside that so we are now hashing the password and then we will just store the password as the hashed password from there so it will the password so we can just create a hashed password five will now cut from here and we need to add here because because here we are creating the new user and the user will contain the password as the hashed password so we can just talk as the hashed password so if I will now save I will now restart the application by running the npm start and then we need to move on to the postman again I will now create a post request is it is a sign up request also we can just create a new request by giving a new field as a Roman and then the email will be Roman and now the password will contain the triple one triple one and if I will now just create a send request again now you can see you will see a different thing in the password and it is contained an encrypted password and now the password is also encrypted of the user and once you will click on the get user again you will get that user as well from the there and now we can just delete these two users as well from the database so we can just move on to the Chrome we can just move on to the mongodb to just start over with the hashed password so to the mongodb so I have just closed mongodb because my application was crashing so we can just click on the sign in again and then we can just remove these two users so here we have the screen and then we can just move on to the browse collections so it will just give us all of the collections that we have in the database right now and it will be the user I think so then you can see the blog is the database name and we have the users and then we have three users currently in the database we can just remove this user from there click on the delete and then you can just delete this user as well and then the James user will also be deleted and then now you can see now we have only one record in the database and now if we will just get the users it will only show one record in the database which is the Roman and then we can just create another user again as it James it will be the James and then if I will now send a new request now you can see now we have the different password of the James so now the sign up functionality is completed and now we need to log in the user as well so create the login route we can just move on to the user routes again create a new route as a router dot post and table the post request again and the URL will be the login so it will be the login route and then instead of the sign up we can just create another controller function to control the login operation and then we can use the login as the const login will be equals to the asynchronous task again as the response request response and the next as the parameters and then we can just create here we can just get all of the fields from the because dot body so we can just grab this line from there and then you can see now we are grabbing the name email and the password and now for logging in the user now we don't need the name so we can just remove that and then we can just only get the email and the password from the request.body and then now we can just move on and we can just check some validation like the existing user is there or not so we can just grab that functionality from there only that we have just created so we can just grab existing user there so we can now just grab that and then we can just paste that here and now you can see existing user is here and then we are now finding the user and then you can see if we are getting the existing user then we need to change this functionality so if we don't get the existing user then we can just send the response dot status as the four of four that we don't find the user by this ID like they don't don't find find user by this email so we can just add that and then after the user is found then we can just compare the password of the user and because the email is now verified the user is there in the database and then we can just compare the password so we have a method inside the big grip to compare the password because we are storing the hashed password so it will be the const as the is password correct so it will be a Boolean function so it will return a Boolean value so we can just use the decrypt dot compare sync and it will just synchronously compare the password so we can just use that so it will be the string that the password and here we can pass the string which will contain the string which we are just sending it will the password and then we need to just add here the compare also and the second parameter will contain the actual password that we need to compare with so it will be the existing user that we have just fetched from the database it will existing user dot password so we will just compare the current password which we have just received with the existing password so we can just add a validation again like if the is password correct is not true then we can just set if it is a falsy value then we can just return the response dot status again until the 400 as the unauthorized and then we can just add a Json and then the Json will contain a message that incorrect password and then if the password is correct then we can just return the response dot status again it will be the 200 which is okay and then we can just send the Json message again and it will contain a message as the is logged in equals to the true or we can just Define here only the message here as the login successful so now we are sending this request and then the function is complete and then we can just return here the function like now we need to just move on to the const login and then we need to just export this login function from there and now everything is fine I think and then we can just move on to the user routes and then we can just get the login function from there and if I will now save and now I think the terminal is stopped so we can just select the new terminal move on to the backend folder at the npm start command and now everything will be fine and then we can just move on to the postman and then we can just close this chrome also so in the postman now we can just create a login so we can just grab this URL and then we can just move on to the new tab and then we can just select in post and then the URL with the login and then we can just grab the email and the password fields from there so we can just grab that we need to move on to the body in the raw in the text instead of the text we need to add the Json and then it will contain a Json and then we are sending the email as the james.com and the password is there a triple one triple one so if the password with the hashed password is correct then we will just see the message as the login successful I think the application is now crashed so there is an error so error event on This Server instance so I think there is an error so we can just restart the application from there and now the application is started again and now we are most request and then we can just create the request by clicking on the send button now you can see we have a message as a login successful so now everything is working fine on the user's end so now the user's functionality is completed and now we need to build the block functionality and for building the block we can just move on to the model we can just create a new model as the block dot JS and it will be the block model now and now everything is working fine as of now so we can just create the model again we need to import the mongoose from the Mongoose we can just create the schema of the collection of the block so we can just have the schema class equals to the Mongoose dot schema and then we can just create a new variable define a new instance of the schema so we can just use the U const as the block schema will be equals to the new schema and then it will contain an object again and now the and now we need to define the fields which will contain in the block schema so it should contain the title of the block so the title can be an object the type can be the string and then it will contain the required field also which will be the true and it will contain the I think it will contain the description as well so we can just copy the title and then it will be the description after that the description it will be the string and the required field will also be true and then it will contain the image like it will the image and this will also be the type of this string and the required path will be close to the true so the image will be type of the string because it will be the type of the URL and then we need to define a which user has posted so we can Define here the user and then it will contain the user and then we need to Define here the user like type will be equals to this string as of now because as of now we haven't defined the block inside the user's model as well so we have to Define there as well so the user will contain the type as the string and it will also be the required field as it true and now everything should work fine and then we can just export this as the export at the default and then into the mongoose dot model again and that will contain the block and then it will contain the schema of the blocks the block schema so now everything should work fine and then we can just create a new routes after that so it will the block Dash routes dot JS so it will contain the routes of the blocks so we can just import the express again from the express.js and then we can just have here the router from the express cons router will be equals to the Express dot router and then we can just grab the router from there and then we need to use here the functions from the router like if the first it will contain the get as always and then it will just get all of the blocks so for that it will be only the main route slash and then it will get all blocks so we can just create that function and for creating the function then we need to move on to the controllers we can just create a controller as a block cache controller dot JS now we have created the blocked controller and then we can just import the blocks collection so it will import as a block from the model slash block and then we can just create a new function as the cons as you get all blocks will be equals to an async function again request response and the next as the parameters again and it will be an arrow function and then for the get all blocks now we can define a variable as if blocks by default it will be undefined so we can just use try catch as always the try catch is here and then we are now returning the console Dot Lock as the error if we will receive an error and then we can just get the blocks will be equals to the I think it will block dot it will be the find so as we have found all of the users then we can just Define the find with the empty query and it will find all of the record from that query and if we can see if the blocks is the empty value or it will be or if it is a falsy value then we can just return the response dot status again it will be the 404 dot Json it will be the message as you know blocks found and then we can just add here the response as the return as the response dot status so if everything is okay then we can just send the status as the 200 and then we can do the Json again the message will be the blocks and then we can just export that or get all blocks and then we can just use the get all blocks from there inside the function as well so we can just import that route and then now we have created the routes of the blocks and the controller and the model as well of the routes and now we need to use here the middleware in the app.js to use the routes as well so here in the blocked routes now we can just export the router variable the export default and now it should contain the blog router because the name will get conflicts so we can just declare the blog router will be equals to the expression router and instead of this router we can just Define here the block router because it will just have the conflict with the name of the routers which are in the users so to the blog router and then we can use the app.js it will just import the blog routes from there it will the app.use it will be the slash API again that we have just defined from there and instead of this user then it will contain the block so now the API slash block and now we can just Define the blog router from there so blog router will be then imported so if I will now save and if I will now just move on to the postman again send a new request I will now grab this URL again but the URL will now be different so instead of this user login so the block it will slash empty request after there I will now click on the send now we should receive an empty array everything is successful status is 200 because now the blocks is truthy value and now we can just Define a new block by giving a post request to the block so it will be we can just move on to the block routes again so we will just close all of the files as of now and then we can just move on and we can just create a blog as the post request so in the post slash add so it will be the URL and then we can just add a block function then we can just create that function in the user's controller in the blocks controller sorry so to the export as the it will be the cost as the name of the function at block will be equals to the async again it will request response and the next as the parameters and then it will contain an arrow function and then we can just grab all of the details from the requested body again like title get to the description it will be the image URL regular image and then we can just grab the user and it will be all coming from the request dot body so we can just Define a new blog instance so to the block will be close to the I think that will be the new block and now we need to import the block from there so we need to just import the blog as well so now we have imported the block and then we need to just create a new instance of this block now like it will contain the title of the block the description of the block it will contain the image of the block and then the user so all of the things will be then there and now we can just save this record inside the try catch block so it will contain the try it will be a block as the it will be the block that we have just created an instance dot save so now we are using the same function this save again and then once we will get an error we will return console DOT log b error and and then we can just add a response at the return the response dot status again will be equals to I think it will be the of 200 but again the Json it will contain an object like it will be the new block that we have just created and now we have now defined everything but now here we need to define the await as well because it is an HTTP task so it should be the weight and then we are sending the ad block function but this is a simple function as of now but it will get complex whenever we will add the relation between the users and the blocks so we can just now save and then if we can see if there is any error so I think there is any error and the error is because in the blog routes so we can just import the ad block from there so now we can save everything should work fine and then we can just create a new request from there so we can just copy the URLs select a new URL select a new tab here the API search block slash add and then it will be the post type of request to the Post it will contain the body as a raw again instead of this text we can use a Json and then it will contain all of the fields so now we need to define the title will be equals to my new block and it will the description description will contain the description like this is my first block and then it will contain the image the image URL now the image URL now will contain sample strings then we can just Define a user as well so to the user the user will contain the string like it can contain the one two three four five so if I will now save and now if I will now just create the send request so now everything should work fine now you can see now we have defined the block the title equals to the my new block the description is same the image URL is now the only the sample URL because it will not be the URL whenever we'll move on to the front end and then it contains a user as the one two three five and it contains an ID so now everything is working fine now we need to create the update and the delete request from there as well so it will be the router blog router again dot it will be the update and it will the put request because the put request is used to update the user and then we can just use here this slash it will do the update block and then we can just add here a function after that so we can just move on to the block controller select a new function as the update so it will be the export const it will be the update block will be equals to again the asynchronous task again it will contain the request response and the next as the parameters and then it will contain all of the things and now we are now defining the update block now we need to find the block and to find the block now we need to add something to the block route so now we can just move on to the update and then we need to get the ID also of the block to update and now after this it will contain an ID so we can just give a parameter in the URLs so it will contain the ID so we can just grab the ID from this route after there so now it will contain after the update we need to provide the ID from the mongodb and now we can just grab the ID from the request.parents and the request.params contain all of the variables which we have defined in the parameters and then we need to grab the variable that we have defined like it will be the cost as the blog ID will be equals to the quest Dot params object dot the name of the variable that we have just defined so it is the ID and if you will just Define it as a bid then you can just Define as a bid from there and then now everything should work fine and now we have the ID so we can just create a new block as the const block and it will be equals to the await because it is an asynchronous task and then we can just find the block so we can just find the block from the block dot find by ID and then we have sub function of the find by ID and update and this function needs two parameters first it will contain an ID so we can just Define the block ID that we have just received and then we can just add the update query so like now the update Fields will only contain the title and the description so now in our in the structure of our application so we can just only update the title and the description of our blog so we can just grab the title and the description from the request.body and then it will contain from the request.body and then we can just add the title will be equals to the title that we have just received and it will the description so now we are defining the title and the description and now we have defined the block so now we need to use it inside the try catch block so we can just Define a variable as a block as the empty and then we can just remove the cost and then we can just add the try catch block and then it will be the block equals to the away description till there and then we can just use the cache also error to return the error foreign so we can just add a validation like if the block doesn't save or if it is a falsy value then we can just return again that response dot status will be equals to the 500 added to the Json Json will contain a message and it will be like the enable to update sub block this will be the message and if everything will work fine then we can just return again response dot status will be equals to the 200 which is success and then we can just send the block that we have just created Now by using the Json with the block so it will just provide us the same description and the title which was already available in the block because it will send the request immediately and it will just send the return statement immediately to the server so now we can just save and now we are now already exporting the update block and then we need to use the update blog here like it will be update block so now the update block is here so now everything should work fine and now we can just move on to the postman we can just create a new route and it will be a put request we can just use here the put instead of this get and then we can just move on and then we can just get the title in the description so we can just grab everything from there we can just select the body select here the Raw select here the Json from there and then we can just add a Json here and then we can just remove the image and the user from there and now the title will contain my new it's instead it will contain and update it block and then this is my first updated block if I will now just send the request now we should see if everything should work fine now you can see now it is a type of request and now we need to grab the ID also from there so we haven't specified the ID so for the ID we can just specify this ID which we have just received by creating the post request so we can just move on to the put again we can just grab this ID we can just add the ID in the URL so now it will just get the ID from there and then it will just send the request to fetch by ID and then update the details of the block so now this will not be the ADD and this will be now the update we can just click on this end so now everything should work fine now you can see it will be updated because this data is the 200 and now as we have discussed earlier it will be just sharing us the same blog which was available in the database so we can just move on to the get again so if I will now just click the request now you can see it will be the updated block because now it is sending the request immediately so it is not stopping us to save a record and everything is now clear and now we can just create a new request at the blog router dot it will the get so we can just create a get request to just get a block from this ID so we can just add a blank URL dot ID and then we can just get a great by ID and then we can just create a function as you get by ID from there as well so it will export const as you get by ID and it will be equals to again the async function it will contain the requests response and the next are the parameters and then we will grab the ID from the request.params that will the ID will be because we request Dot params.id and then we will grab the block from there that will be the Black Block will be the undefined we can just use the try again try cash block it will contain an error and then we will just return the console.log the error and then inside the try we can just create a block will be equals to the block model that we have created Dot find by ID so it will just find the block by its ID and it will return that block from this ID so we can just Define here the ID find by ID and then we can just add here the await also because it is an HTTP task and then we can just check validation like if we don't have the block and then we can just return the response dot status again dotted with the 404.json message will contain that no block found and then after that we can just return your new statement as a return response dot status will now be 100 and then we can just send you the Json as the block so we will just send the block from there and now if I will now save and if I will not just move on to the blog routes import the get by ID block so now this block will contain a same URL slash ID and then we will just get the blocks from its ID so if I will now save and if I will now just create a new block from there and then if I will now just create a post request again now we will have the two blocks in the database like there and if I will now just grab the ID of the second block let's create a new request from there like it will be the get request again from the block or we can just provide here the ID here the ID on the send request and then you can see it will show us my second new block so now everything is working fine as of now now we can get the block from its ID and we can just update the block from their ID and then we can just delete the block from this ID as well so we can just create a new request at the blog router.delete it with the delete request and then we will just grab the ID from the delete so we will grab the ID and then we will just create a controller function to delete a block and then we can just create a new controller function as the export const as it delete block and it will be again the async and then request response and the next and then it will contain an arrow function and then we will just grab the ID again from the URL the ID will be equals to the request dot param subject dot ID field and then we can just create a new blog as a block there and then we can just add the try catch block once again it will be the block will be equals to now we have a function again it will be the await it will be the block dot find by ID and remove so we can just use this function to find by ID so it will just find the block by its ID and it will remove it from the database so we can just use that find by ID and remove we can just pass here the ID which we have received and it will just await for that and if you just delete it then we can just use the error we can just console.log the error again and now we can just use here if the block is the falsy value once again we can again just return the response dot status dot it will do the 400 and then we can just send a new request to the Json and then we can just add here the message again as you unable to delete and then I think it should be the 500 and now if everything is correct then we can just return response dot status it will be equals to the 200 success and Dot Json it will just send a message again as this string and it will contain the successfully delete it if I will now save and now we have just exported the delete block also and then we can just import the delete block from there as well we can just save and then we can just try again everything is working fine and then we can just create a delete request after there so the request will be there like this ID is there so we can just grab this and then we can just add a delete request there so it will be the same ID and then we can just add here as a delete and then the update will not be there and if I will now just send the request by deleting it send in your request I think there is an error and the error is because we haven't specified the ID there so it should be the call on ID to declare a variable and if I will now save and if I will not just try again click on the send request again now you can see the successfully deleted now the blog is successfully deleted and once I will just grab this block from its ID click on the get request block so if I will now just give this ID to is so now we can see that the no block found from this ID so now everything is working fine from the users and from the blocks perspective and now the thing which is remaining is a relation now we need to relate the user with the blocks and the blocks with the user to do this now we have one thing inside the Mongoose so we can just provide the reference to the collection of the blocks from the users and vice versa from the blocks to the users as well so we need to move on to the I think the block and then we can just provide the reference to the user so we have defined it as a user so it will not be the type of this string it will be type of the Mongoose dot we have the types namespace and then we can just have here the object ID so we have the ID of the user from there and then we can just specify the object ID from there and after this we need to specify one more thing we need to specify a reference so the ref is used to provide a reference and then it will just provide a reference inside the string so we need to define the collection name so the collection name that we have just created the model it will be the user so it will just Store The Collection as the user so now we have defined the user as a type because of the Mongoose DOT type of object ID and we have specified the reference of the U user and now the thing is that each block will contain only one single user so each block will now they have the parent of the one single user but each user can have the multiple blocks so here we are only defining only the single user per single block but when we will move on to the users now we can Define here the blocks field it will the blocks but it will be an array because the users can have multiple blocks inside that and array will contain an object it will be the type and the type will be same as the mongoose DOT type schema dot object ID so we can just define object ID again and then we can just Define a reference also it will contain the reference to the block the block is a collection and it will just contain the reference and then it will be the required Fields also it will be required it will the true so now everything should work fine as before but now we need to change few things now we need to move on to the user's controller and once we are creating a new user like in this sign up it will be having a blocks because now we have now defined the required field of the block so it will just have some blocks inside that so once we are defining the user as the name and the password then we can just Define here the blocks will be equals to an empty array so it will just store the blocks as an empty array so if I will now save and if I will not just move on to the sign up request of the user it will be like this now we have the sign up request and then we can just create together as a nickel it will contain the email as a nickel at the redtest.com the password is the one one one one one one but if I will now click on this send now we would see two things now you can see now we have the name as the nickel the email as this the password is the hashed now we have the blocks field and now everything is working fine because now we have the blocks field there and because now this is only a sign up request so we have just empty blocks array so now everything is working fine inside the users and now the users is completed and now we need to move on to the blocks so we have the blocks controller and then once we are creating a new ad block now we need to add the user as well so now this time the user will contain the ID so before everything by creating the ad block now we can just create a validation we can just create a existing user again like that existing user is available or Not by default it will be undefined so we can just add a try catch block again and then it will be having existing user will be equals to will be equals to the update again the input the user dot find by ID so we will just add here the user.find by ID I think the user is not imported so we need to import the user as well because now we have relation between the two documents so we can just add the user dot find by ID and then we have to grab the ID of the user and now the user will contain the ID so now this user field will contain the actual ID of the user and now we have defined the user and now we can add a cache block also to specify an error and just return the console Dot Lock the error and then now if the existing user is true then we can just verify that like if if the existing user is like the falsy value and then we will just run the return and the response dot status and it will be the 500 or you'll add the 400 unauthorized dot Json will contain a message again message will be the like the enable 2 find user by this ID we can just send this validation message and if everything is working fine then we can just create a new blog as the block and now we are already saving the block but now instead of directly saving the block now we need to add the block to the users as well and for that now we can just remove that await and now we can just add some session from there so for that now we can just Define here this session and it will be equals to the await again as the Mongoose that we have the session in the Mongoose dot start session there so we can use that so it will just start a session and then we will just do every single thing from this session and then we will just about this session so then we can just have a session variable and then we'll be having a start transaction so we will just start the transaction from there and then we will just first we will just save the block so to the block that we have dot save so it will just save this block from calling the save function from there and then we will just use the existing user that we have so the existing user now we need to push to the array of the existing user always so it will existing user dot blocks that we have and Dot push so so this user will contain the existing user so the existing user will contain from the user that we are just finding and and it will just push the block to this array like it will be the block and then we are referring to the block and then we can just save also and then we can save also from there it will be I think of it again await from there it will existing user again dot save so it will just save but now we need to add a session because now we are saving the user and now we need to add this session always so it will the session and here also we need to define the session because now we need to save the user from this session only from this session object only so we need to Define this session there as well and there as well so we are now defining the sessions as a key value pairs again and then if everything would work fine then we can just use a weight as well it will the session dot commit transaction so now if everything would work fine then we can just use the commit so now after this transaction would be committed so now this is a whole complex thing because now we are declaring a session because the sessions contain the start session then we are now defining a transaction and then we are now saving the block from this from the same session that we have just created and then we are now sending the blocks to the existing users array and then we are now up saving that user also that block also and now now we are committing the session if can it contains an error we can just console.log error and then we can just return the response dot status will be equals to the 500 dot we will just send the Json again and it will contain a message let's see error itself that we have just received so it will just send the error and now if everything will be fine now we can just save that I think we are now declaring the existing user finding the user by its ID if error we are now logging the error if no user then we are sending the message and then we are now defining the new user as a new block and then we are defining a session to save the block start the transaction and then save the block and save the array and then push to the array of the user and then we are saving the users array as well and then we are committing the transaction so if everything would work fine now we can just save and we can just try it out again we can just move on to the post request again of the blocks so it will be the post registrative request of the block it will be the ADD and now we need to define the main user that we have so we can just use the get users get all the users from there but now you can see we have now all of the users from there and then we can just use here this ID so it will be the James user we will get the ID of the James and then we will just use this ID instead of this one two three four five which is a sample so if I will not just save and if I will know just click on the send now you can see now once we are sending the request now you can see now it contains a block but now it contains a title as the my second new block the image will be the sample image but now it contains the user's field as well and the ID of the block that we have just created but if you will just move on to the get users again click on the send once you will just move on to the James now you can see the James has the blocks array and it contains a ID of the block so now you can see it is now related and now this ID contains in the end it contains a double F and then we can just move on to the post request again it will be the double F so now you can see everything is now related and now everything is working fine properly and then now we can just save that user also from this update block and in the update blog I think everything would work same as before because we are now just updating the block but now the next complex thing is now we need to delete the block so for deleting the block the block will be deleted from the blocks collection but we need to remove the array we need to remove the block from the user's array as well so we can just again get the block from the ID so we can just get the block from his ID we are now removing the block from its ID and then we are now returning the status if the block is not found and now we need to do one more thing here now once we are sending the block so then we can use here the populate method to populate something so the populate method will allow us to populate something from this object so we will get the user so it will just move on to the user's collection so populate works for the reference Collections and it will reference to the user and it will just find the details from the users as well so now this block will contain the block and the user's object as well and then we have to do one more thing so it will be the await and then it will be an asynchronous talk so it will await as well and then we need to just remove the ID from the user's blog array so it will be the block that we have just defined and then as we have discussed earlier it will contain the user object and then it will contain the block of the users as the user blocks and then we have a method in the Mongoose which is the pull so we can just use that pull and it will just remove that block from there so we can just pass that block from there so now you can see now we are not defining the block equals to the await and defined by ID and remove and then we are now removing the block from the user's array as well so if I will now save if I will now just try out deleting that block now we can see now we have the delete request and now this block is already deleted so we can just get the block that we have just created so we have just created this block I will grab this ID we can just move on to the delete once again we can just add this URL from that the WF URL once I will click on this send now everything is should work now you can see the message is successfully deleted and once I will get that block from there like it will the James so we can just click on this end now you can see and now the blog is not deleted because we are not saving the user after creating a pull of the user so there after we are creating the pull when we are removing the block from the user then we need to save again between the block dot user dot save again so if I will now save and if I will now just try again so we can just create a new post request of the new block so I will now just add a new block with this user like if I will now just add the user with the double FB so if I will not just send now we should see the success message now you can see the status is 200 which is okay and then if I will now just get the users like 11b it is a Roman if I will now just send the request now you can see this is the new block which we have just created and then now we can just copy this blog ID and then we can just move on to the delete and add this ID here in the URL and click on this send now you can see the successfully deleted and once we will move on to the get again send the new request now you can see the block is now removed from there as well so now the delete request is also completed and now everything is successfully completed and now we are saving the data saving the blocks with the user and then once we are deleting the blocks and then you can see the user's block is also deleted and now everything is completed but one thing is remaining that now we need to create a one more route inside the blocks and that cloud will be getting the blocks of the users so for that we can just create a new get request and inside that we can just create a slash user slash its ID so it will be the slash user ID and then we can create a controller function after that also and for that we can just move on to the blocks controller again and then we can just create a new function as the const will the get by user ID it will be close to the asynchronous again as always request response and the next as the parameters and then we can just create the function and then we can just get the ID first so it will be the user ID will be equals to the request dot params object dot ID as the value and then we can just create a blocks of the user as the user blocks will be the by default it will be undefined and then we can just create again the try catch block so the try catch block will contain the user blocks will be equals to the await and after the update we can just find the blocks of the user from the user model so we can just use the user model again and then we can just find by ID of the user like it will just find the user by its ID by giving the user ID there and then we can use here the one word function and the function there will be the populate so the populate will now be used here and it will just refer to the collection of the blocks and it will just get the data from the blocks so we can just populate the block from there and now it will just give all of the blocks for only this user which have this ID and then we can just add a cache statement after that it will be an error so we can just return console DOT log the error so now everything will be completed and now we can just add some validations again it would be if we don't have the user blocks or if it is a falsy value or like there is some error and then we can just return again it's done response dot status will be equals to the 404 that not found dot Json will be there like message that the no blocks found here and then we can just create here a return then it would be the return as the response dot status so we need to add the status now as the 200 that everything is successful and then we can just send a Json message again and this time the Json message will contain the blocks and then it will contain the area of the blocks like we have the user blocks so if I will now save and if I will now just move on and we can just export this and then we can just use this get by user ID inside the blocks routes as well it will be the get get by user ID we can just import that and then we can just save and then we can just try so we can just move on to the postman once again and I will now just create a one more block for a user and then I will just add this ID inside the post so this will be my new block again so the user will be there like it can be the nickels block I will now just save and send the request now the blog will be created and then once we will get the request now you can see nikhil has one more block now nikhil has the block and now we can just get a block by the ID of the nickel so we can just create a new get request so it will be the http so we can just get the URLs there because the URL is almost same so we can just get that and then we can just paste this URL here everything should work fine so instead of this API slash block then we can add there the user slash it will be the ID of the user so we can just copy the ID again we can just get the ID again click on this send now we should see the blocks of the nickel so I think there is an error once again so we can just see the error like cannot populate the path of the block so I think we are we have missed the S after that so we can just move on to the blocks controller once again and then it would be the blocks instead of just a blocks so we can just cancel and then we can just send that request again and now everything should work fine now if I will now send the request now you can see now we are getting the blocks with the ID and the details all of the details of this and now you can see we have the blocks and then we have the array of the blocks you can see we have the array so now everything is completed and now we have completed the back end
Info
Channel: Indian Coders
Views: 96,557
Rating: undefined out of 5
Keywords: nodejs backend tutorial, nodejs app for blog application, nodejs from scratch, node express mongodb tutorial, nodejs express api tutorial, node js express tutorial, nodejs express and mongodb tutorial, nodejs project, nodejs crud project, node express mongodb crud, relation between multiple collections in nodejs and mongodb, add multiple collections in mongodb, how to add multiple collections in mongodb with node and express, how to create backend with nodejs
Id: _ee38nL13mE
Channel Id: undefined
Length: 83min 10sec (4990 seconds)
Published: Mon Oct 24 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.