 Hey what is going on everybody, this is going to be a detailed video explaining what exactly you need to do in order to make your Node.js MongoDB project work on Cyclic, I'll create a quick project from scratch and lead you step by step and this will be a fairly fast pace video. I'm also making a shorter version which I will link here, I hope that you find this video useful. Let's start by creating our project, I'm gonna be using the GitHub desktop application for this but feel free to use the terminal or whatever you're comfortable with. So I'm gonna click here, current repositories, add create new repository, insert here, I'm gonna put the name of Node.js Cyclic-MongoDB, I'm gonna initialize a README file and also I'm gonna put the license as MIT license and then I can create the repository. Open it in Visual Studio Code and the first thing that we need to do is to create a .getignore file. So new file .getignore and inside here we need to ignore two files, the first one is .env which is gonna hold all connection string and then on Node modules. Basically we don't want to be uploading this folder to GitHub and we also don't wanna upload or username and password on GitHub so everybody can see. Save this and close, let's create all .env file as well, so .env like so and we should be good to go. Let's close that and let's initialize a new project, terminal, new terminal and then inside here I'm gonna do npm init-y and this is gonna create a new project for us and let's install some of the dependencies that we need. npm i for install and then we need express, mongoes and .env, let's enter and this should install the dependencies. All right, now that we're done let's close the terminal and let's open the explorer. The next step is very important, your main file needs to be called either index.js or server.js because otherwise it won't work so in this case I'm gonna go with index.js. Inside index.js is where we're gonna initialize a new express server and do all the database connections and route. Let's start by requiring .env like so and then this is gonna be config. Now we need to require express and mongoes so const express equals choir and then inside here we do express and we also need const mongoes equals require and then we require mongoes like so. Now we need to create a new express application and to do this we can do const app equals express close this and then we need to set a port number for which our application is gonna be running on so const port equals and then inside here we can use the environment port number which is gonna be very helpful for when we launch your app it's gonna be using the server port number so in this case we can do process .env.port or if we're working locally we can put all 3000. The next thing that we need to do is all connection string to mongo db first of all we need to put this code here mongoes.set trickery comma false because we're gonna get a lot of warnings inside the console which is not what we want so add this line here and then we can do all connection so const connect db and then this is gonna be an asynchronous function like so and inside this function we're gonna have a try catch and inside the try is where we're gonna try to connect to the database by doing const const and then equals await and then we're gonna await for mongoes and then inside here we're gonna do connect and I've actually made a spelling mistake here so let me change this super quickly mongoes connect we're gonna bring a string from the .env file here which I'm gonna create in a second so what we can do is process .env and then mongo underscore uri like so and we should be good to go now if we connect to the database it will be good to know so I'm gonna do console.log and inside here I'm gonna do mongo db let me remove this and then connect it and then we're gonna put with a dollar sign by the way these are the slanted single quotes so we can do template literals like so so con.connection.host but if we don't connect we just wanna console.log the error so console.log and then we console.log the error and then also we can do process.exit one like so and we should be good to go so this is our database connection ready and now we need to create our first route so this could be our homepage so for example we can do app.get and inside here is where we put the URL so in this case this is gonna be our homepage I'm gonna leave a slash and then after this I'm gonna do comma we're gonna have the request and the response like so and this is gonna be a narrow function and inside here we can render something or we can send something so for example rest of send and I'm gonna send something like title box done we'll test this in a second so this is very specific to cyclic here but we need to connect to the database first and then start the application so in order to do this we can grab the connection from here then another function like so and then if we connect we can then do app.listen and we can listen to the port number and this is gonna be another function like so and inside here we can do console, log and then we can do listening on port and then if we do dollar sign we can put the port like so but in order for this to work we need to put the slanted and as you can see it will be highlighted okay this is the very basic of it all but if we run the application now obviously this is gonna break because we can't really connect to the database without the database configuration in order to do this let's jump to the explorer and let's click on dot tmv and now let's create our MongoDB database go to MongoDB.com and if you haven't yet got an account click here try for free and then you can sign up in here by adding your first name last name company email and password or you can sign up with google if you wish I've already got an account so I'm gonna click sign in okay once you log in your dashboard should look something similar to this on the project and then we can click new project so I'm gonna call this one cyclic like so and then I'm gonna click next project owner can stay the same create project and now we can build our database click build a database and now for the database we can click on the free shared one here scroll down to the region close to you I'm gonna choose island and scroll down create cluster and now we need to create or username and password for this I'm gonna put ready and then I'm gonna generate a password so for the password we need to copy this and let's put in our dot tmv for now so I'm gonna paste in here and let's go back so we can create or username here perfect we have ready with the password that is now hidden and if you scroll down a little bit more you will see this IP address with cyclic if we go back to cyclic and if you go under docs.cyclic.sh how to using MongoDB for the free TS cyclic does not offer private networking which means that we need to allow by using this IP addressing here so copy this and then you can add an inside here also this is usually under the networking tab if you can find it and then add entry finish and close go to database now the next thing that we need to do is click on connect and then let's go to connect using VS code and this is gonna give you this string in here copy the string paste there inside d.emv for inside index.js we created this process dot emv Mongo URL this is the name that we need to copy and we need to put in here and this Mongo underscore URI is gonna be equals this string here that we just copied paste in here and this is our password that we copied earlier so I'm gonna grab it and replace the password in here as you can see my username is already added so that's absolutely fine and the last thing that you need to know about this is that this is gonna be your database name if you wish to interact with the database you will need to create your own database model in order to do this you can create a folder called models and then inside the models for example we can create a new model called books.js and inside books we can create a new model I'm just gonna super quickly copy and paste one just so you can see but in this one we have cons Mongoose equals require Mongoose and we export in this model in here so we create a Mongoose schema and this is basically a book model where we have the title of the book which is the type of string required it's a field that is required and then we have the body of the book which is also the type of string and is required now that we have this model you can pause and copy it super quickly we need to insert this into or index.js const book and then equals require and then inside here we can require it's gonna be dot slash and then models and then the model that we want is books now we can use this model to interact with the database all right let's create a new route and to do this it's exactly the same as this here so what I'm gonna do is create a new route app.get add note is gonna be the route and inside here I want to insert a couple of books so we're gonna do a try catch because this is an asynchronous function and inside the try I'm gonna insert books so for example we're gonna be using the box model that we just inserted so we're doing await books insert many and inside here we can add the books that we want to insert title and body just like our model sons of anarchy body text goes here title game of thrones body text goes here and that's it so two books is what we're gonna be inserting and if you get an error we're gonna do console.log error plus the error and now let's do one more which is gonna display the box so if you scroll down a little bit I'm gonna do app.get slash books async request response and inside here we can do the query which is const books equals await.find this is finding all the books basically after this we can do if we get books rest.json so we want to display the books in a json format else something went wrong tidy this up super quickly so terminal new terminal and then node node index.js now errors will get and then if I go to the browser we'll get now what we need to do is commit this project to github so basically upload your files to github and I'm gonna do init and then I'm just gonna commit all the changes that we just done publish repository and then you can either keep this private or you can make it public if you wish whatever is up to you I'm gonna keep mine private and let's press publish repository now let's jump back to the browser and open Cyclic and if you haven't yet got an account I will paste the link in the description below but all you need to do is click sign up and this will give you a sign up form here and you can only sign up with github which is good so your github account will be straight away connected but I've already got an account so I'm gonna press sign in and then I'm gonna click now let's deploy our application click deploy inside here we need to click link your own and then we need to search for the repository which is Cyclic and as you can see it's not finding it because it's private if it wasn't it will find it straight away but if it's private you need to click add a private repo then this is gonna open this window here and we need to scroll down a little bit you can either allow access to all repositories or only the selected ones I'm gonna do only selected and then I'm gonna add the new one that we just added so cyclic so no gs that cyclic MongoDB here and it's added to my allowed list and now I can approve and install now we have our application added in here and all we need to do is click on this here don't forget to do that and this is gonna fetch the repository and install it on the server perfect we have all your own here but before we go to the URL we need to do one more thing and if you go back to the cyclic dashboard and if you click on your project which we just created here so click on the config icon here click on variables we need to add our MongoDB URI in fact they've already got some default variables like this but you can also add custom ones if you wish to so if you go back open the .emv file and copy the Mongo underscore URI like so and paste this variable inside here and this is gonna tell you basically what we just done that they do things differently and close this and let's say if we go back to overview now and if we click on the application URL here you will see that application is working we have books and or other route was the route that inserts books so add note I'm gonna do that before I do that let me show you that in our database we don't have any collection so I'm gonna do browse collection and as you can see it's created the collection just now I think but we don't have any data so what I'm gonna do now is go under here and do add note which is gonna hit the route which is gonna add the notes okay I didn't render this so it's gonna break make sure that you do rest or send and then you send a new same book on it save them then obviously it's gonna say service unavailable because I didn't do that but hopefully the data should be you know database now so if I refresh you will see that we get the two books I added and also now if I go under books you'll see that we're getting the two books that I added in our database so we're making a query and we get in the box that's pretty much everything from this tutorial I hope that you found it useful don't forget to click the like button and give me a comment below and hopefully I will see you in the next one bye