# M2 - Heroku Deployment (with Mongo Atlas) <br> ### 1. [Signup for a Free MongoDB Atlas account](https://www.mongodb.com/cloud/atlas/lp/try2)  <br>  <br> ### 2. [Sign Up & Create a Free Cluster](https://docs.atlas.mongodb.com/tutorial/deploy-free-tier-cluster/) 1. Sign Up for Mongo Atlas - enter the email, password, first name, last name 2. Select >>> **Starter Clusters (or Shared Clusters)** 3. In the drop-down **Cloud Provider & Region** select: - Europe Region 4. in the **Cluster Tier** select: 5. In the drop-down "**Cluster Tier**" make sure to select: - **M0 Sandbox** - with the flag **Free forever**. 6. Click the button **Create Cluster** <br>  <br>  <br>  <br>  <br> ## 3. Setup MongoDB Atlas Cluster: 1. In the sidebar in the **SECURITY** part: - select **-> Database Access** <br> 2. Click on **<u>+ ADD NEW USER</u>** to create a new user - Select **Read and write to any database** - User Privileges - Set the **Username** and **Password** - Set **Database User Privileges** to : ***Atlas admin*** - Create New User <br>  <br> 3. In the sidebar select **SECURITY -> Network Access** <br>  <br> 4. Click on **<u>+ ADD IP ADDRESS</u>** - Click on **ALLOW ACCESS FROM ANYWHERE** - Confirm <br>  <br> 5. Connect To Your Cluster: Click on the gray button **CONNECT** - Select **Connect Your Application** option - Choose Your driver version: **DRIVER**: Node.js - **Copy** the **Connection String Only** <br>  <br>  <br> 6. Update the connection string and add the ***username*** and ***password***. <br> ## 4. Create a Heroku account https://signup.heroku.com/ <br> ## 5. Install Heroku CLI #### [LINK: Docs - Install the Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli#npm) <br> ## 6. Login to the Heroku website and *<u>Create New App</u>* #### Open your [Heroku Dashboard](https://dashboard.heroku.com/apps) 1. Go to [`dashboard.heroku.com/apps`](https://dashboard.heroku.com/apps), and then select: **New** >>> **Create new app** >>> **Choose region - Europe** >>> **Create app**. 2. After you create the app, you should be redirected to the app's dashboard. 3. Select the **Deploy** section. <br>  <br> ## 7. Connect Heroku app to the local repository <br> ### Follow the Deployment instructions - *Deploy* section <br> While in the Heroku app *Dashboard* open the ***Deploy*** section <br> To login to Heroku and create a new Dyno, from your server/project root directory, run the following commands (also mentioned in the ***Deploy*** section) : <br> > <sub>In the below example the angle brackets `< >` indicate the identifier/parameter/argument values that are provided by the user. You should omit them when executing the commands.</sub> ```bash # Login to heroku from the terminal heroku login ``` <br> ```bash # Add heroku remote heroku git:remote -a <name-of-the-newly-created-app> # Check the remotes available # Newly added `heroku` remote repository connection should be shown git remote -v ``` <br> ```bash # Commit the most recent work on the development branch git add . git commit -m 'Write a clear meaningful commit message here' git push origin <development-branch-name> # Checkout to the master branch git checkout master # Merge (bring) updates from the development branch git merge <development-branch-name> git push heroku master ``` <br> ## 8. Set *Config Vars* in Heroku <br> 1. In the Heroku **app** **Dashboard** go to >> **Settings**: 2. Click on the **Reveal Config Vars** 3. Create the new keys for the environment variables: - `SESSION_SECRET` - `MONGODB_URI` - Set the value to be the previously copied **Connection String** from Mongo Atlas. **Remember to add the** `username` and `password` **to the Connection String** you created in Mongo Atlas. <br> For the sessions secret: <br> ```bash SESSION_SECRET=s0m3RaNd0mS7r1nG123 ``` <br> For the newly created MongoDB database in the Atlas Cloud. <br> > <sub>In the below example the angle brackets `< >` indicate the identifier/parameter/argument values that are provided by the user. You should omit them when executing the commands.</sub> ```bash MONGODB_URI=<Mongo Atlas Conection String with username and password> ``` <br>  <br> :heavy_exclamation_mark: Double-check that all needed config variables (`MONGODB_URI`, `SESSION_SECRET`, etc.) were added properly during the previous step : *Heroku app **Dashboard** **>>** **Settings** **>>** **Config Vars** **>>** **Reveal Config Vars*** <br> ## 9. Update the project files <br> 1. Check that `mongoose.connect` URLs in the `app.js` and `bin/seed.js` (if you are using a seed) are updated and using `process.env.MONGODB_URI`. #### `app.js` & `bin/seed.js` ```js require('dotenv').config(); // <--- ADD // ... // ... mongoose .connect( process.env.MONGODB_URI, // <--- UPDATE {useNewUrlParser: true} ) .then((x) => console.log('Connected to the DB') .catch(err => console.error('Error while connecting to DB', err)); // ... ``` <br> 2. Update the `package.json` and [specify the version of Node](https://devcenter.heroku.com/articles/deploying-nodejs#specify-the-version-of-node): - Run command `node --version` to get the version of Node.js installed: ```bash node --version #> v12.4.1 ``` <br> - Add the `engines` config section with the `node` version to the `package.json`. For example, if your node version was `v.12.4.1` you should set the following: ##### `package.json` ```json "engines": { "node": "12.x" } ``` <br> 3. After updating the `mongoose.connect` connection strings and adding the `node` version make sure to `commit` the changes and push to remote on GitHub (`origin`) and Heroku (`heroku`) ```bash git add . git commit -m 'Update mongoose connection and add node version' # Push the commit and update the version on GitHub git push origin master # Push the commit and update the deployed version on Heroku git push heroku master # Open the app in the browser (from terminal) heroku open ``` <br> ## 10. Heroku commands ### Logs To fetch your app’s most recent logs, use the `heroku logs` command: ```bash heroku logs ``` The `logs` command retrieves 100 log lines by default. You can specify the number of log lines to retrieve (up to a maximum of 1,500 lines) by using the `--num` (or `-n`) option. ```bash heroku logs -n 200 ``` <br> ### [Troubleshooting - Real-time tail](https://devcenter.heroku.com/articles/logging#real-time-tail) If there was an error during the deployment, you will be shown the following screen prompting you to run `heroku logs --tail` command:  <br> Real-time tail displays recent logs and leaves the session open for real-time logs to stream in. This command is used to troubleshoot any deployment errors when the deployed app is not working as expected. ```bash heroku logs --tail ``` <br> ### Access the terminal of the Heroku container We can open the terminal instance on the Heroku container ([dyno](https://www.heroku.com/dynos)) in order to run custom scripts or see the files included in the instance. To do this we use the command [`heroku run bash`](https://devcenter.heroku.com/articles/heroku-cli-commands#heroku-run). <br> For example to **run the seed sequence and populate the production database** we should do the following : ```bash # Open the terminal in the app dyno in Heroku heroku run bash # We may then run the seed file node bin/seed.js ``` <br>