Creating a New App on Heroku with MongoDB Lab
To start, we need to create a new app on Heroku. This can be done by going to the Heroku website and clicking on "Create a new app". The app name is important, as there can only be one app with each given name. In this case, we'll call our app "anyCard". Once we've created the app, Heroku will guide us through the process of setting up the app.
The first step is to log in to Heroku and run `heroku login` to open up a new browser window where you can enter your credentials. After logging in, Heroku provides instructions on what to do next. We'll follow these instructions to create a Git repository for our app. The first step is to initialize a Git repository by running `git init`. This will create a new directory called `.git` that contains all of the files necessary for our Git repository.
Next, we need to add Heroku as a remote host for our Git repository. We can do this by running `git remote add heroku
We'll also need to commit all of the changes to our Git repository using `git commit`. The first commit will include all of the files that are currently in our Git repository. After committing, we can push our changes to Heroku using `git push heroku master`.
Now that we have a new app on Heroku, we need to set up our database. We'll use MongoDB Lab, which is a free service provided by MongoDB that allows us to create and manage databases in the cloud.
To add a new database to our app, we can run the command `heroku addons:create mongodb`. This will create a new database for our app and connect it to our Git repository. After adding the database, we need to set up our environmental variables. We'll do this by going to the Heroku dashboard and clicking on "Settings" under the "App" tab.
In the settings page, we'll click on "Reveal Config Vars". This will show us all of the environmental variables that are currently set for our app. Our database URI is automatically generated when we create a new MongoDB Lab database, so we can leave this as is for now.
The only remaining variable that we need to set is the database name. We can do this by copying and pasting the section between the slashes and the colon from our database URI. This will give us the exact name of our database, which we can enter into the "Database Name" field in the settings page.
After setting the database name, we can click on "Add" to save our changes. Now that we have a new app on Heroku with a MongoDB Lab database set up, we're ready to start building our app.
Testing Our App
To test our app, we'll log in to the Heroku dashboard and go to the settings page under the "App" tab. We'll click on "Reveal Config Vars" to show us all of the environmental variables that are currently set for our app. We can then enter these variables into our app's code.
To test our app, we'll open up a new browser window and navigate to the URL `https://anyCard.herokuapp.com/secret`. When we arrive at this page, we'll see a text input field where we can enter a name. If we type in "bo" and click on the "Submit" button, it should display an error message saying "Not found". However, if we try again with a different name, such as "quincy", it should also display an error message saying "Not found".
If we want to see our app in action, we can add some placeholder data to our database. We can do this by creating a new file called `data.js` in the root directory of our app. In this file, we'll define an array of cards that we can use to populate our database.
We'll also need to update our app's code to use the `client.close()` function instead of `db.close()`. This is because our database is a MongoDB Lab database, which uses the client library to interact with the database. If we don't use the correct function, it will throw an error and prevent our app from functioning properly.
After updating our app's code, we can commit all of the changes using `git commit` and push them to Heroku using `git push heroku master`. Once we've pushed our changes to Heroku, we'll be able to see our app in action by navigating to the URL `https://anyCard.herokuapp.com/secret`.
To use our app, a user will need to pick a card from their deck and enter it into our text input field. If they do this correctly, our app should display the correct data for that particular card. However, if they make an error, such as entering an incorrect name or no name at all, our app should display an error message.
Conclusion
In this tutorial, we created a new app on Heroku using MongoDB Lab. We set up a Git repository for our app and connected it to Heroku, then added a new database to our app using the `heroku addons:create mongodb` command. We also updated our app's code to use the correct functions to interact with the database.
Finally, we tested our app by navigating to its URL in a browser window and entering different names into the text input field. We saw that our app displayed error messages when users entered incorrect data, which prevented it from functioning properly.
Overall, this tutorial demonstrated how to create a new app on Heroku using MongoDB Lab and how to set up a Git repository for your app. It also showed how to update your app's code to use the correct functions to interact with the database, as well as how to test your app by navigating to its URL in a browser window and entering different names into the text input field.