Learn How to Start a Node.js Server & Connect to MongoDB Compass | Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys welcome to OCTA color in this video lecture we are going to learn node.js how to start the server and we are going to learn how to connect to the mongodb database so let's get start so here you would just make any folder on your laptop on your drive so I have already created the folder name I given as a octa app as you can see so first of all if you are a beginner person so first of all you need to install the node.js on your system so I am assuming that everyone already installed the node.js I already installed that so everything is ready then then just create the one folder on your drive and open that folder in the vs code so here you will learn every single line of code from the scratch so I am going to show you step by step all the process so please keep watching so first of all we are going to create the project we need to initialize the project so for that just go to the terminal open the terminal and then you have to initialize the project the command is npm npm means node package manager and in need in it is the command and then enter so as you can see here whatever the package name you want to give here you can give it it is the suggested to as OCTA app so you can give as OCTA app if you give like this then it will show you the name can no longer contain capital letters so you have to write it all small letters OCTA F so I am going to give as this is this is the suggestion they have given so you can write that or if you don't write it it will auto be the OCTA have packaged Center version is one so let's enter description this is uh server start and database connection this is whatever the description you can give it here and then enter entry point index.js okay and no problem for the index.js which some people use the server.js some people use the index.js so no problem for that and test command nothing git repositing nothing keywords nothing author is OCTA coder enter ISC so see you will get the this information if all is correct then just enter and then when you enter here you will get the package.json file here let's see please watch here you will get the new file automatically and see package.json file is gone whatever this information is written here is all written in that file as you can see see all the information are we have given here the project is initialized now the acid gas nothing else so here for the server you need to import the express so for that we don't have here the dependencies so we have to install that dependencies also so for installing the express Express is a library so that's why we need to install here inside the node.js so for that we have the command npm install you can write it all installed or you can just write it I and then Express then hit the enter package is installing now you will get here node module and there is another new file package log dot Json we don't need to change these files and nothing else and here in the package.json you will get the dependencies here you can see the express version current version is 4.18.2 that is the current version so that's it nothing else so here we need the I'm going to create new file as a index.js or you can create as a server.js whatever the name you want to give it and I am going to minimize this tab so we need to import the express inside the index.js so for that we have to const express equal to require so that's this is the expression imported it imported from the node module we just install the dependencies see so we imported that Express from this node so that's it and we need another variable so for that we are using apps so inside the app we are using the express methods so that's it guys so here we are just going to start the server first time so for that we have so just give the server server listening code starts here so here we have the method and Dot listen you can get here the suggestion not the listener account listeners not this we just need the listen and then the port and then whatever the port number you want to start the server you can write it here so 3000 I'm going to start the server on 3000 port and then the functional start and you can see console and you can check inside the console the weather server is started or not so for that we have the command console.log if the server is started then successfully listening on Port 3000 save this file so for the running index.js file so we have when we have to give the command node index .js and then hit the enter and see successfully listening on the port 3000 and you can open in the browser localhost 3000 cannot get which means the server restarted if you queue the wrong URL thirty thousand one and hit the enter you will see 3001 is not this site is this is not the server we are running so you can change here you can give any server port number like 8080 save this file and Ctrl C is the server stopping this command whatever the running project is it will stop here if you press the Ctrl C and then if you just hit the up Arrow you will get this same command here and then just enter it successfully running listening on the port 3000 because we didn't change but it will change we have written inside the string this string we didn't change but the port will start on the 8080 let's check it refresh this it was before 3000 so let's try see 3000 is not working so let's give it a 8080 and hit the enter so see cannot get it means we didn't given the API so the server is started successfully so that's how to start the node.js server as you can write here also Port 5000 and that Port name you can give it here and give the back take so this will be great not that and here dollar and curly prices and port it will automatically so we already started the server but it did not talk so Ctrl C press the command it will stop the server and up Arrow it will start again node index.js and see it's listening on the port 5000 because this is we can write it here also or you can change it from me you can write it here also server name server port number and whatever the you comfortable so this is the another way so that's that's how to start the server guys so let's check here also refresh the file see it is 8080 so it is not going to work give it a 5000 enter so it will be working so that's the correct way to start the server app Listen is the application start listening for the HTTP request on a port whatever you have given the port numbers for that we use the listen method so that's how to start the server and now we are going to learn how to connect the mongodb data base in this app so we are going to learn the connectivity of the database so for that we need the mongodb and Mongoose for the clear all this command we just need to click CLS this is it will all delete the command reports so here we also need to install npm I and Mongoose is for the database connectivity so install that Mongoose first so the packets are installed here as you can see the packages dot Json file dependencies Mongoose you can see here the current portion is a 7.1.0 close this file we don't need to change in that file and now here we are going to import the mongoose cost Mongoose and required the Mongoose so save this file server could always give it in the last lines like this so and between them we have to write all the apis the routes and Etc so here we are going to write the first we are going to write the database URL DB URL so equal to the URL is we need to copy from the mongodb come pass the URL you have to copy from this mongodb Compass see this is the URL mongodb compose URL so copy that URL and paste it here and then give the name for the database so I am going to give the OCTA this that's it here I am going to show you how to connect the database on the mongodb compass on your local machine or if you want to use the online mongodb Atlas so instead of this URL you have to put here at last URL there will be the password also so you have to put that we will learn all that in the next lecture so for this now I am going to show you in the localhost how to connect the database on the Local Host so for the Mongoose database connection we have another method Mongoose dot connect and this is the path DB URL whatever the path you have given Mongoose dot connect method is used to connect the mongodb database using the DB URL whatever this URL we have given here it is inside the DB URL and Mongoose dot connect is the method to connect that to the mongodb database so here we are going to use another variable dot connection this is the method we may spell here const and then con dot once and then open con dot once method is used to listen for the open event which is emitted when the database connection is successfully established when the event is fired the console.logs will show the message successfully database connected save this file and then here we also need to write the if the connection is not successfully connected if some error happen so we need to write connect dot on method and then error the con dot on on is the method is used to listen for the error event which is emitted if an error occur during the database connection process so it we are going to print here error connecting to database this will the message will print and then the application we need to terminate otherwise it will skip running so we need to terminate the process for that we have the method exit so that's it guys database connection is also done so let's check it is working or not node index.js hit the enter now we have the error Mongoose dot connection is not a function yes that is the right we written here as a function it is not the function we made the mistake it is just connection save this file and run the node index.js file again and you will see successfully listening on the port 5000 and hopefully it will connect to the mongodb if I think it is not connecting so it will throw an error let's see and we got the error connecting to the database here this happens lot of times that's the that's why I am going to show you how to connect this database in a correct way because we don't have the permission for this localhost those your system is stopping for that so for that we have to write here 0.0.0.0 4 times 0 this is the IP address you have to write it for the local machine let's try again and now now let's run again the index.js file time and see boom successfully database connect and that's it guys that's the error you will get when you copy this URL it will still not connect because your IP address is not matching and your system is how the Securities so that's why it is not connecting so let's try we how we are already running here 45 less tree yeah it is working and now I'm going to show you how to create the route so for the route we have the method app dot gate and whatever the path you want to give let's give it it is a home page so for that home and request and give the parameter and callback functions and that's it then result dots and let's give it inside the HTML format welcome to OCTA coder so that's it nothing else just save this file you can see I just did the Ctrl C and I'm again starting the server here again so all these connectors successfully less reload and let's give the home the URL we have given and see we got the welcome to the OCTA coder so thank you guys thank you for watching this is the correct way to start the server and connect to the database this is the main problem people are getting they don't know what to do here they are giving every URL is correct but it is not still not connecting so it just because of the IP address of the local machine so for that we have to write the 0.0.0.04 time you have to write it 0 and it will connect if you are using the mongodb compass so for that you have this kind of URL and this is the database name and if you want to use the mongodb atlas then you can take the URL from the mongodb atlas you need to provide the password there so that's it guys so if you like this video please subscribe this October YouTube channel and you will learn everything from the basic so thank you for watching
Info
Channel: Octacoder
Views: 2,876
Rating: undefined out of 5
Keywords:
Id: WwDKWZn7lKQ
Channel Id: undefined
Length: 18min 32sec (1112 seconds)
Published: Sun Apr 30 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.