Intro/Creating a Database with MongoDB and testing it on Postman
Tags: javascript, beginners, node, codenewbie
Published: 2021-11-20 22:24:53

Hi! My name is Samantha, a brand new software dev! 
	
I have been learning to code for five years and this year I joined a bootcamp to get a structured curriculum and have a community to lean on when I’m stuck and need to see things from a new perspective.

My goal with this account is to practice what I learn by creating examples and talking through it. I hope to be able to reflect and see my progress and learn new techniques.

If you are also a new learner, I hope this helps you. Aight, let's get into it! 

By the end of this post you and I will be able to:
build a server using node.js and express
adding routes
testing those routes using Postman
adding data to a database using MongoDB

You’ll want to start by creating a directory (folder) with your project name. Mine will be fav-shows and inside of fav-shows i will touch (create) and file named server.js . Once they are created, I will open Visual Studio Code and start! :D

![Terminal Start](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pyefrl00mpc1tns3flsl.png)

Before I start typing anything, I’m going to run node.js and install express in the VSCode terminal using npm init -y  and npm install express.

![Installing node and express](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vedu21tcsxvug7pd0izh.png)

Next, we’ll set up our basic server. To do so we’ll call express, assign our port, and then tell it to listen. You’ll notice a console log and this is for the purpose of making sure we set up our server correctly (this again will show up later on). To start our server we will use node server.js(or nodemon). 

![Setting up server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kebz7xvm70lyqakvh5ib.png)

Now we install mongoose and navigate over to MongoDB to gather our DataBase URL and connect it to ‘server.js’. (NOTE: Please do not use the URL I have listed.. this is an example. You will go to MongoDB’s website, create an account and follow the steps for setting up your first “cluster” and getting the connection URL)

![Installing Mongoose](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tcwtdofsddxt5vudauyr.png)

Okay, yay!! We did it and now it’s time for the fun stuff… We’ll mount our middleware and add our routes, but before we do this, lets create a schema. 

This will go in a new folder called ‘models’ and the file name will be show.js (in my example, yours may be something else). A schema is essentially a template. This is the information that our database will be collecting. Since mine is about my favorite shows, I’ll do the title, year released, and the starring actor.

![Creating Schema](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/asraae8lpn0zgnrgsnjv.png)

We can now add out middleware and routes. Each route will have a specific purpose and they go as follows:

Index - will gather all the data in the database
Delete - will allow us to delete a piece of data from the database
Update - will allow us to update any data in the database
Show - will show the data that we choose from the database

![Adding routes](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9nj4ae3d0nuepcehj50w.png)

This is the final stretch, with everything that we’ve created so far, we can now test our app on Postman. Postman allows us to test each route and add the items to our DB based off the schema we created. So we’ll start with the create route.

![Postman POST route](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i2arj9yw9b5k1h73xe83.png)

Boom! We added something to the database. You can see here that we used POST, the method we used when we wrote the create route. We then typed out our url and highlighted on body, where we entered in our key values and their pairs. To test the others we would follow what we have written for each route. Another example could be the index route, which will show us all of our entries.

![Postman GET route](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nsnoz2isrld322fxkg03.png)

Yay! All of the entries are there! Notice I changed the drop down from POST to GET and I did not change anything else. I only hit send. 

I've created three databases since I learned this and the practice has really helped me understand how each piece of code works. I hope this helps you too.

Like I always say, feedback is welcome. I will always be a student, even when I graduate and start my career as a developer. 

Now congratulations on creating your first database! :D