Node.js Blog App REST API with MongoDB

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my friends today we are gonna create a blog application rest api using node.js and mongodb we are gonna create an express server then we are gonna create a login and register routes we will be able to update and delete user create a new post update and delete it and add new categories and also you are going to learn how to use query to fetch all posts for example we are gonna fetch posts and filter them by user or category and finally we are going to create an upload route and we will be able to upload an image and store it inside our server if you didn't watch the previous video we created a blog design using react i highly recommend you to watch that video because in the next tutorial we are going to use that design and this api and we are gonna create a complete blog application i hope you will enjoy it if you want to see more projects like this you can subscribe to the channel so if you are ready let's get started okay firstly i created here a folder which is client and i moved everything that we have done in the previous lesson this is our react application so i'm going to create one more folder here this is going to be our api it's going to include our express server our roots models and mongodb connection so let's come here this terminal and i will say cd api so inside this api folder i'm going to install my framework and libraries to do that you can write here npm i or if you are using yarn yarn add firstly it's gonna be our express server to connect our mongodb i'm gonna use mongoose to connect this mongodb we are gonna take a private url from this page and it's gonna include our password and username so we should hide them somewhere inside this folder so nobody can reach them to do that i will install dot envy and finally we are gonna upload some images to do that i'm gonna install malter oops space here and i'm gonna enter during the installation i'm gonna explain you this mongodb you can create free cluster and you can create your collections as you remember if you watched previous videos we created some travel app and social media app so we are gonna do the same thing so let's go to clusters again and here i'm gonna click connect you can use terminal or desktop application but we are gonna use inside our code so i'm gonna choose second one and this is our secret url i'm gonna copy this and this is our username and it's going to be our password so how we are going to reach this you can go to database access and here as you can see we have a user if you don't have you can create new one if i edit this as you can see we have a password here so i'm gonna use this password inside that url okay let's go to the vs code again okay it's ready so what i'm going to do firstly i'm going to create my index file index.js so let's create our express server i will say const express and it's gonna require express i'm gonna create my application const let's say app and express okay to use that i should listen any port so i will say app.listen and i'm gonna indicate here my port number you can write here anything i will say just five thousand and after that i will just console lock this connection i will say backend is running so how i'm gonna run this application firstly i should install my node.js because if you go to package.json we have only dependencies here we don't have any application yet npm in it so it's gonna ask some questions package name version description i will just enter them is this okay of course and tada it's ready so to start this application you can write here i will delete this test we are not going to test anything and here i will delete this if i write here note index.js and if i write here yarn start or mpm start it's gonna work as you can see it's working perfect but if i make any changes here let's say for example console.log hello and i'm saving as you can see nothing has changed that because it's not listening our application but we have an awesome library i'm gonna kill this process and i'm gonna install yarn at nodemo okay let's go to package.json again and here i will say nodemon index.js i will save and again let's start as you can see right now it's listening our application if i change here for example llama as you can see it's running again this application we don't have to write anything we don't have to start again and again i have some so let's close here and here so let's try our application in the browser i will say app dot use and i'm gonna indicate here a url let's say main url and after that i'm gonna use my request and response and i will say console.log hey this is main url so let's go to the browser and here i will say localhost and my port number here as you can see hey this is my url if i change here for example lama hey this is lama url i'm refreshing page nothing has changed if i go to llama awesome so right now we have a rest api we can reach any url but writing this inside index.js it's not a best practice that because we will have authentication user operations category operations post operations so what i'm gonna do i'm gonna create here roots of these operations for example for user for authentication for post something like that but before let's delete this and connect our db as you remember we copy our url so what i will do i will create my envy file and inside this dot eme file you can hide any url any secret case anything you don't want someone to reach them so what i will do i will say url or whatever you say and i'm gonna paste this url and here i'm gonna choose my tb let's say blog and here my username and password let's delete here and write lama again okay it's ready so how i'm gonna use this emv file i should do my configuration to do that i'm gonna just call this.me dot env and it's gonna require dot eme so if i come here and say dot eme dot config okay right now we can reach this url let's copy this and after that of course we should import mongoose first so i will say const mongoose and require mongos okay so let's look at our documentation here i can close this this is mongoose website and here as you can see we have a connection method here so i will copy this and paste here so i should change this url how i'm gonna use my emv file i will say process and eme and right now i can paste here my mongodb url url and by the way something wrong with this mongoose version so i should write here use create index true it's not crucial but if you don't want to see any error here i highly recommend you to use this and it's gonna be true okay so after that if we connect this url i will say then and i will say console.log connected to okay so if there is an error i will say catch and i'm gonna console lock this error so let's try ok perfect connected to mongodb so i can showing this terminal here and inside this api folder let's create our models and roots i will say models and roots okay let's decide what we are gonna create which models firstly we are gonna have users let's create here user.js and second one will be our posts post.js and category as you remember i wrote here app.use and our url to use them i'm going to go to roots file and i'm going to create my url routes so first one will be users in this case we will be able to delete or update our users we are going to use this url and second one will be posts we can create new post delete them update them and categories and one more when i create a user i don't want to use this url because it's not a user operation so i'm gonna create here authentications so you can write here off dodges okay so let's close them and start with these users so i'm gonna import mongoose again const mongoose and require mongx so firstly i should create my user schema so i will say const user schema so i will say new mongoose and i'm gonna create schema and here my object first one will be our username each user has its own username and here type will be string and it's gonna be required it's gonna be true and i will say unique true so nobody can create a user with same username and after that it's going to be email again type string and required and unique i will just copy them and here i will say password type will be string and it's going to be required again and one more thing i'm gonna add profile picture for each user so i will say profile picture and it's gonna include image url so it's gonna be type string again so it's not gonna be required because when you create a user you don't have to upload any image first so i will say default and it's gonna be empty string so one more thing i will do you can create here create a date or updated date but there is a shortcut for this a new object and timestamps and i will say true so it's going to create our updated ad and create that times so finally i'm going to export this schema so i will say module exports and mongoose model my model name will be user and finally i'm gonna pass my schema user schema okay that's all so let's copy this and let's go to the post test and i'm gonna paste here let's change this name it's gonna be post schema and here okay so let's delete them so what we are going to need here firstly we will have title it's going to be string and i will say required true and i will say it will be unique i don't want any post with same title okay and after that we will have description so i will copy here and paste here we can have post photograph so i will say photo and again but it's not gonna required let's write here false or you can delete here doesn't matter and after that i'm gonna create username so we can see the author and finally i'm gonna choose my categories so it's gonna be type array we are gonna push all categories inside this array for example we'll have array and let's say live music something like that so let's delete this and required will be false and one more curly brackets here and comma i forgot comma here okay perfect so our post model is ready also i will copy this and i will go to category let's change these names it's gonna be category okay i will delete them so it's gonna include just one property it's gonna be name type will be string and required true okay that's all actually i can delete this timestamp or it can stay doesn't matter it's not that important so i will close these models and let's go to these routes to create router we can use our express framework so i will say constructor and it's gonna be require express and i'm gonna call router method here okay so which model i'm gonna use in this authentication route i'm going to use jav's user so let's import this const user and it's going to be required i will go to models folder and inside i will use user okay firstly it's gonna be register and one more is gonna be login so i can use this router here let's say router and which method i'm gonna use it's gonna be get or paused or put so if you are creating something it should be post method if you are updating existing model you should use put method if you are deleting some model you can write delayed or if you are fetching some data if you are not changing anything or creating anything you can use get method so we are gonna create new user so i'm gonna use post method and which url i'm gonna use it's gonna be register it's not gonna be a normal function because when we create a new user it's gonna connect our database and try to create new one and it's gonna return us a response so all of this process takes some time and there is no way to know this duration so this is basically an async function so i will write here async and my request and response so what's this request and response this is basically what we are sending to our server when we create a user we can send username email password everything inside this request so what's this response after this whole process it's gonna return as a response it can be a user or just warning or you can return any string you can just say okay creating user is successful something like that this is our response so i'm gonna create my function here if you are using async function you should create try and catch block because we are gonna try to connect our db and try to create new user and all this process can be fail so in this case we are gonna catch this error and you can do anything with this error but in this tutorial i'm not gonna deep dive into error handling i will just return same status so i will say status 500 which means something wrong with mongodb or our express server and after that i'm gonna return this error and here what i will say currency i will say const new user and it's gonna be new user model and inside this model i will say take everything inside this request if i say buddy it's gonna take whatever we sent here we can send user name password as i said but additionally even if you send something not related with this user for example if you send title it's still gonna take this property that because we are seeing everything inside to prevent this you can indicate here your own properties you can say username request and body and username and two more email and password so in this case it's gonna take only username email and password so how i'm gonna save this user i will say const user and i'll say wait because it's gonna be an async process so i will say new user and save that's all so this same method is coming from our mongoose that because we are using here our user schema so if everything is successful i can send a response i will say response and status will be 200 which is successful so i will say json and you can write here user has been created or you can send this user directly i will send this user so let's test this route how i'm gonna do this i will open here my postman application it's an application that you can make any requests for your api it's actually really heavy nowadays i'm considering changing this postman but it's still really popular and probably you already have this application so i can use this doesn't matter so i will say new request so first request will be register and here you can create your own collections let's say here block and here i'm gonna choose my request method it's gonna be post so here i'm gonna write here my localhost 5000 so how i'm going to reach this url we can't reach right now because we didn't indicate inside our index file let's go to index here so here i'm gonna indicate my router i will say const authentication route route it's gonna be require and my authentication path going to be roots and earth okay so how i'm going to use this route let's come here after this mongoose and before this listening post i will say app.use and here i'm gonna indicate my url firstly i will say api you don't have to write this you can say anything but i prefer writing here api so this url will be off and here i will say my router of root okay that's all i'll save okay something is wrong okay router use require same middleware i think we didn't export our router here yeah i should export this first i will say module exports and it's going to be router let's see again as you can see it's working perfect so right now if i write here my url it's going to be api and auth and here i'm going to choose body as you remember we said request and body this is this body and here i'm going to choose row and here inside this json object i'm gonna write here my username and it's gonna be let's say suffix and here by the way you should choose json it's colorful right now which means everything is okay and here email and supply gmail.com and one more thing it's gonna be password i will say one two three four five six okay so if i send this right now it's not gonna work let's try okay this was not i was expecting that because we didn't write here register but one more if i send it's not gonna work again as you can see we have an error here that because this application is not able to send any json object inside body to prevent this error i will come here and say app.use i will say express and json so in this case we can send any json file and a json object so let's try again i will send as you can see 200 okay successful and my new id username email password created an updated app perfect if i go to my cluster here let's choose this my collections as you can see block users and inside my new user but there is a problem here we can see this password we are not supposed to see this password that because it should be always private if someone reads your database they can see every password and they can use them for some bad intentions something like that so however i'm gonna hide these passwords let's come here and open new terminal i will enter into my api and here i'm gonna install a library it's going to be decrypt i will say yarn add bit crypt let's write here and here mpmjs as you can see it's an hashing library that we can safely store our passwords and here as you can see it's an async function we should require this and we should indicate salt rounds and after that we can hash our password so let's do that let's make this smaller and here inside all i will import this library and after that let's come here and here i will say cons salt is gonna be a wait because it's a synchronous function so i will say pcrypt and ginsault and you can write here any number as you can see in the example it's 10 that's right here 10 and after that i can create my hashed password i will say hashed pass and here again i wait i will say pick ripped and hash function and inside i'm gonna indicate my original string and this salt i will say request and body and password that we are sending here inside this body and we are going to hash this with this sort so in this case this is our new password so i can delete this and paste this here and i will save let's come here and again i'm gonna try with another user let's say john and i will send okay it's successful let's look inside as you can see our password is hashed right now so even if someone reached this password there is no possibility to find our original string perhaps i will refresh here okay awesome so let's delete this one it's not secure okay perfect so we finished our registration let's come here and take care of this login process so i will say again router and i'm gonna use post method again like we did here because we are gonna send again our user information this time it's gonna be logging url and here async function request and response and again my try and catch block error i will just send this as response and status will be 500 and json sent this error so what i'm gonna do here firstly i'm gonna try finding our user i will say const user and i'm gonna use my model and i will say find one that's because it's unique user there is only one user with one username and email so here i'm gonna write my condition which user i'm gonna try to find i will say username will be request and body and username so basically we are going to add our username here insidebody and he's going to try to find this user inside mongodb ok so after that you can write here if there is no user inside rdb you can write response dot status you can say 400 wrong credentials and here you can send wrong username but i don't recommend you because generally we don't say user which credential is wrong so you can just write here wrong credentials okay so if there is a user we should validate our password because when we write here for example this password we should compare this with this hashed password if they are the same the user can log in okay so how i'm gonna do this i will say const while date and i'm gonna say await again by the way i forgot writing here wait and again we clipped this time i'm gonna use compare methods first one will be request body and password which we send and second one will be user.password because we've hatched this user inside our mongodb which is this user and this is user.password so we basically compare them and again if it's not validated that's right here validated actually and again i will copy this if it's wrong password i'm gonna send this wrong credentials again so if everything is okay i can send this user i will say response status 200 and json user so let's try i will write here login and let's delete this email firstly i'm gonna write here wrong username john 1 i will send as you can see wrong credentials so this time it's gonna be correct and this one will be wrong again wrong credentials so finally they are gonna be correct one and as you can see it returns this user but i wanna show one more thing to you we are sending this password i don't want to send this password to user so how i'm going to prevent this i will come here and say const i can take anything inside this user for example password or username or email everything and if you want to take others you should use spread operator and i will say here others you can write anything here doesn't matter and here i will say user and i should write here document but before let's try this one i will send this others so in this case it's not gonna take passwords it's gonna take other properties if i do that i'll save and i will send again as you can see there is something strange here that because we can't take these properties directly inside this user so in this case i should use this document let's write this here dot document and again i will save and now i'm gonna send as you can see we have everything but password awesome so we can also log in right now so we finished our authentication so i'm gonna copy this and let's go to users here we are gonna use this user model again we don't need any hash methods and here i will delete here and let's delete this actually also okay so what i can do inside this user so we can do everything except this authentication so we can update our user and we can plate this so in this case as i said before when we update something you can use put method so this time it's gonna be main url and here i'm gonna indicate my user id to do that i will write here column and id so basically we can use this user id and find user and then we can update them so firstly i'm gonna write here a condition i will say if request embody and user id because we are gonna send this user id inside this body like this and it's gonna be our user id for example and we are gonna send this to this url users and some id here so basically it's gonna compare this url with this user id if they are not same it means we are not owner of this user so in this case we can't update this because it's not our account by the way we are not using jwt i want to make another video for json web token and after this video our applications will be more secure but in this case it's just enough i think i will just compare them if request and i will say params because it's our parameter here params and here id don't forget this is request also but it's not buddy we are not sending we are taking from this url okay so if it's equal i will move this here so if it's not i will say s i'm gonna say response status it's gonna be 401 which is you are not allowed i will say json you can update on your account oops okay perfect so if everything is okay if they are same number we can use try and catch block and update this but there is a problem here i deleted this big grip but we are gonna use this because we can update our password also so i will say again pick ripped and require is gonna be pcrypt so i will say if we are sending any password inside our body i will say if request embody and password in this case we should hash this password again i will do quickly const salt pcrypt and hash and gene sold it's going to be 10 and this is async function of course so i should await this and after that i'm gonna hash my password i will say request and body and password in this case i'm gonna change this value it's gonna be await and be crypt again and hash and previous password request body and password and my salt here i did everything we have done before in this case i didn't write here const password it's totally same thing doesn't matter i just change this value okay right now i can go inside this try catch block and here i will say const updated user and i'll say wait because we are going to reach this user inside our db so i will say user we have a useful method i will say find by id and update so in this case i can indicate here my id which is request and params dot id this parameter and after that i can update my user i will say i'm gonna use here as hat method what i'm gonna set everything inside request and body so basically if i send here username and it was john for example i'm gonna write here john and updated and comma and my password will be one two three so i'm gonna take everything inside this body so after that if everything is okay i'm gonna send this user response status will be 200 and json is updated user let's try users and it's going to be put method and here let's take this user id it's going to be john and here i will leave this like that and let's test it again same mistake because we didn't use inside our index let's do this quickly i will copy this and this time it's going to be user root and it's going to be users and here again i will duplicate this and users i set user or users ok user here this time i can use it let's try as you can see you can update only your account because it's not our user id so i will change this also they are same right now i will send everything is okay as you can see nothing has changed here but if i check here i will refresh as you can see john updated and our different password so why i can't see this here that's because of our mongoose so if i see my updated version i should come here and say new and it's going to be true so in this case it's going to update this user and send this new user updated one not old one let's save and try again i will come here update it to for example and send as you can see our username here updated too perfect so let's lay this user i will copy this and here this time it's gonna be delayed method again we will have this user id async function and again i will check whether this id belongs us or not so i can say you can delete on your account and here we don't need this password we are just deleting and this also so what i'm gonna do here i will say await and user model i will say find by id and delete here and here i'm going to indicate my user id request params and id so after that it's going to delete this user and send us a message user has been deleted okay but there is a problem here if we delete this user we can still see this user's posts so what i'm gonna do i'm gonna delete all posts of this user so firstly i'm gonna find this user and find all posts and after that i'm gonna delete posts and this user so what i will do here i will write another try catch error so let's move this here firstly i'm gonna find this user const user and await user model and i will say find by id by id and here my user id params.id so if i can't find this user i can send here respawns and status this time it's going to be 104 because there is no user like that so i will say json usually not found so if i find this user right now i can delete this and it's posts so i'm gonna import my posts here post model by the way you don't have to delete all posts maybe you wanna still keep these posts just like this user whatever you want by the way let's delete this okay so however i'm going to delete this post i will come here and say await and post model and i will say played many that because we can't have many posts belong same user so i'm gonna write my condition here i will say username user dot username as you remember our post model we have a username here so in this condition we are going to check this username if it's same username with this user inside db it's gonna delay them okay so let's do this i will say delayed and this username and we are sending our user id here sorry i said username it's user id i will send as you can see user has been deleted there is no posts but it's gonna delay this user i will refresh here yep there is no user anymore so what else i can do here i can get just one user i will say get user so in this case i will say router dot cat and it's going to be again id we are going to fetch this user by using this id async function request and response i will use try and catch i'm gonna send this error and json error and here i'm gonna try to find this user const user it's going to be await and user model find by id i will indicate my param id and after that again i don't want to send every information because when we fetch any user we are not supposed to see its password so i will do the same thing const password and others it's gonna be user.doc so i can send this others response status to hundreds and json user sorry others so let's create some user and see register and here let's delete this joan gmail.com it's gonna be email let's send oops it's delayed method it should be post because we are registering okay right now i will change this to get method and here it's going to be users and user id let's take this and paste here i will send as you can see 200 successful and it returns this user but i can still see this password something should be wrong here ah that because this typo here i will save and again i will send as you can see it's here perfect so we've finished our user router i will copy them and let's go to post router i will paste here this time i will delete this maybe we are gonna need this user i can keep this if we don't use we can delete and first one will be create new post and update post and and too late post and get post so i will delete this in this case it's going to be post method because we are creating new one and here i will say const new post is going to be new and our post model and inside we are gonna take everything request and body so i will say try and catch response status will be 500 and json is error so so i can save this new post i will say const save post it's gonna be new post and save so i can set this response status will be 200 and it's gonna be saved post okay let's come to index file and here it's gonna be post root and here i'm gonna use this route posts and post route let's come here i will copy this user id and here i will say posts and post methods and inside this body i will say username okay we are using username not id so it's going to be john updated and here i will say title title it's going to be test we have description and it's going to be test description and what else that's enough i think let's try i will send 200 okay but we can't see anything let's check here posts that because i didn't use a weight here it's still waiting again if i send this time it's going to be 500 that because our title is same because we sat here we can't use same title here because it's unique so if i come here and change this as you can see it's here so let's check here and we can see our two posts here okay this is user our posts as you can see test and test tool perfect it's really easy so what else i can do i can update my post so let's delete here and it's going to be put method and this is going to be our post id and here let's create our logic firstly i'm going to find my post and then i'm going to compare post username and our username here insidebody if they are same we can update if they are not it's not our post and we are not allowed to update that but i will say try and catch and here i will say const post i'm gonna find this first signed by id and i'm going to say request and params and id so in this case if we have an error i will say response and status i will say again 500 and json error and after that and again i forgot this await it's really important and again i will say try and catch this time i'm going to validate our user id and here let's say if post a username equals request and body and username it means it's our own post so we can update this actually we can write this if block here that because we are not trying anything okay so else i'm gonna say response and status it's gonna be 101 and i will say you can update on your post so what i'm gonna try here i'm gonna say const updated post so i will say await and pause and find by id and update again i will say request params and this post id and after that i'm gonna set my new properties so i will say set that i'm going to set it's going to be request embody everything inside this body and again i will say new here otherwise we can see this updated post it's gonna be true right now i can send this updated post i will say response that is 200 and json updated post so if there is an error i will just copy this and paste here so let's try it's going to be posts and put methods and here is going to be post id let's check here this test2 and here username first it's gonna be wrong one let's say three and title will be test to update it and everything else will be the same i will send you can update only or pause because it's not our post if i change this as you can see title test to update it everything else same and this is our new updated time perfect so i can copy this and paste here right now we are going to delete it's easier it's going to be delayed method and again if they are not same i will say you can delete on your account sorry post and here i will just say wait so i can use this post here and delete by the way you can write your find by id and delete and you can but we already found this post we can delete this directly you can do the same thing here doesn't matter so here i will say post has been deleted so let's see this time it's going to be delayed again let's test it i will change this you can delete on your post let's write your correct one post has been deleted let's check here [Music] it's gonna be only this test okay perfect we deleted this test too so what else we can do here i will say get post again it's going to be paused awaits pause find by id and after that i can delete this because there is no password or something and i'm gonna directly pass this post so let's see i will take this post id here and in the postman i will say get methods and posts and this id i'll send and it's here test test description and john update 2. it was really easy one and one more thing i can get all data all posts now we need this because when we visit our website we should charge all data all posts so what i will do i will copy this and paste here this time it's not gonna be id it's gonna be just main url in this case we are gonna fetch all data but i'm gonna add some condition here because when we write here main url we are gonna fetch all posts and then we write here for example question mark and user and when i say it john it's gonna fetch all data but only it's gonna check this username it's gonna be just john and one more thing it's gonna be here and category and let me say for example music it's gonna fetch all data but only category name is music it can be complex right now but you're gonna understand don't worry so how i'm gonna catch these queries i will say const username it's going to be request this time i'm going to say query it's going to look at question mark and after that first value will be our query and after equals it's going to be our value and let's say user so i will do the same thing for category cut name and it's gonna be just shortcuts cat okay so right now i'm gonna create here a pause array that because we are gonna return this array as response so i will say let posts i'm saying that because it's changeable we can change inside everything so i will say let's delete them actually i will say if there is a username posts will be i will say await and post model i'm gonna find and here my condition if username equals this username this query you can write this column and username also but after as6 if they are same name you can delete this it's exactly the same thing so this expression equals this expression same okay so if there is category name not username i'll say as if by the way you can write here just if or as if conditions or switch case doesn't matter there is not only one way so i will say if there is category name it was cat name here so this time our posts will be again await post and i will say find and this time our condition will be categories because remember our post model here we have a categories area here so i'm gonna try to find this category name so how can i say if this array includes this category name it's really easy i'm gonna check here in methods and here i'm gonna indicate my category name so it basically says look at this categories array and if inside include this category name just find this and assign to this post ok so i will say s if there is no username no category name it's gonna just fetch all posts there is no condition i will say post it's gonna be post model and just find so right now i can return this response status will be 200 and it's gonna be json posts now if you didn't understand just watch one more time this part i'm sure you're gonna understand it's not that complex so let's try but before i'm gonna create another post here [Music] and this time will be let's say chain from jane and description will be same doesn't matter okay let's get this post i will say send okay something is wrong cat methods oh i forgot a wait why i always forget this i don't know okay again i will send and as you can see we have posts from jane and john so i can write here my conditions my queries i want to fetch all posts but only belong to j i will send and this time we see only this post it's gonna be only john what was the name john update 2 i will send this time we only see this post okay we can't test this category because we don't have any inside these posts but doesn't matter if it's working it works also i don't want to waste time so we've finished our posts also let's go to the categories here and finished our rest api i will come here and i will copy this [Music] and i'm gonna export i don't wanna write them again and again okay this time it's gonna be categories sorry category this model as you remember we have only name here so it's going to be really easy and i'm not going to delete or update i'm going to just create new one and get all categories so i will say router and post method it's going to be main url we are just going to create async function request and response i will say columns new category new cut and it's going to be new category and inside i will say request and body as we did before and i will say try and catch error again response status 500 and oops sent this as json so what i will do here i'm gonna just save this data and send this i'll say const saved category it's gonna be await in my new category and i'm gonna just save and after that i'm gonna send this status to hundreds oops and json and saved cat saved cats it's really strange okay that's all let's create new one but before let's go to do index file and here categories it's gonna be category root and here categories and again categories and category route let's try it's gonna be categories and post method it's gonna be only name here name i will say music let's create as you can see it's here let's check here and categories and music is here perfect so one more thing i'm gonna fetch all categories i can copy this and paste here it's gonna be cat noted i will delete this and here i will say cons categories and category and find there is no condition and after that it's gonna set these categories if i say get and send as you can see we have only music here perfect so one more thing i'm gonna do here when we create any post here we are sending our description title and categories but what about host image how i'm gonna upload new image so i'm gonna come here this terminal and i will say yarn add a motor i'm gonna enter so this is a library that we can upload our files of course you can use firebase or amazon aws or whatever any cdn but for basic practice you can upload inside your api firstly i'm gonna create here a folder you can say whatever you want i will say just images and i'm gonna upload every file inside these images so let's close them and here i'm not gonna create any routes for this uploading file instead of this i'm just gonna write inside my index file it's gonna be really easy one i will say const melter and require multi so firstly i'm gonna create a storage i'm gonna indicate this images folder so how i'm gonna do this after this mongodb that's right here const storage and i'm going to use my moulter here and and i will say disk storage and inside this i'm going to indicate my destination it's going to take three parameters first one will be request and second one our file and third one our callback function and this callback function is basically take care of our arrows if there is any error it's gonna take care of this so i will say call back and first one will be null and second one will be our destination which is images so after that i'm gonna indicate my file name and again request file and callback and again null and you can create your new file name but we are gonna send this file name to our react application here client so what i will say i will say request and body and name oops i forgot this arrow here okay perfect so basically it's gonna take our file and it's gonna save inside these images and file name will be the name which we are providing so how i'm gonna upload this file i will say const upload i'm going to use motor again this time i'm going to indicate my configuration it's going to be storage and this storage will be our storage here this one okay i will say app.post because it's gonna be post method we are uploading something so i will say api upload we are gonna use this url and after that i will say upload and it's gonna be single because we are gonna upload only one file and its name will be file and i'm gonna write here request and response i'm going to say respawn.status it's going to be 200 and json file has been uploaded so let's try i will go to postman and here my url will be upload and here post methods and file name will be let's say my image let's look at here and take this extension it's jpeg and don't worry we are using this postman only for test purpose after that we are gonna use our react application and we are gonna create a file name logic and after that i will choose here form data and k will be file because our k here is file [Music] that's right here so here it's not going to be text it's going to be file let's choose our image here i'm going to choose this one and if i send okay there is something wrong because we can't use here our body so let's write here for now not request body name just for manually hello.jpg and i will save and send again as you can see file has been uploaded and here inside images this is our image so why i did this here that because in the postman we are sending our data but we can't send this name here so instead of this i wrote here manually but inside our client site we are gonna take this request body and name so so far everything is perfect it works so that's all i think we finished our api here let's delete this image if you are confused about this milter don't worry after client side you are gonna understand better so i can close this we can create new user register and login we can create post with image we can delete them update them also so let's close this and close this postman so we finished this project i hope you enjoyed it if you learned something please like the video and if you want to see some more you can subscribe the channel so that's all i hope i will see you in the next video goodbye you
Info
Channel: Lama Dev
Views: 58,159
Rating: undefined out of 5
Keywords: node rest api express, node rest api tutorial, node.js, node.js rest api, rest api, rest api tutorial, node.js mongodb, nodejs mongodb api, blog application, blog app rest api, blog app with node and mongodb, node mongodb projects, mongodb, express, node.js login, node.js register, node mongodb login, node js mongodb authentication, node.js mongodb crud
Id: OML9f6LXUUs
Channel Id: undefined
Length: 75min 11sec (4511 seconds)
Published: Wed May 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.