Easy Guide to Deploy an Express-Mongoose App on Heroku
Deploy Your Express-Mongoose App to Heroku: A Step-by-Step Guide
Deploying your Express app with a Mongoose ORM to Heroku can seem daunting at first, but by following these steps, you'll have your app running on the cloud in no time.
1. Navigate to Your Project Directory
Open your terminal and change the directory to your project folder:
cd /myProject
2. Install the Heroku CLI
Install the Heroku CLI using snap (for Linux users). This tool allows you to manage your Heroku apps from the terminal:
sudo snap install heroku --classic
3. Set Up Heroku Remote
Add Heroku as a remote repository to your local git configuration. This enables you to push your code directly to Heroku:
heroku git:remote -a <your-heroku-app-name>
Replace <your-heroku-app-name>
with the name of your app on Heroku. For example:
https://enigmatic-ravine-23031-1df92bd6df4a.herokuapp.com/
Clidk open app to copy exact url:
Example : heroku git:remote -a enigmatic-ravine-23031
This command adds a remote named "heroku" pointing to your Heroku app.
4. Deploy Your Application
Push your local main
(or master
, depending on your branch naming) branch to Heroku:
git push heroku main
5. Managing Configuration Variables in Heroku
Heroku allows you to manage environment variables using Configuration Variables (Config Vars). These variables can store sensitive information such as database URLs, API keys, and other secrets, keeping them safe and separate from your codebase. Here’s how to set up a database connection string as a Config Var in Heroku:
Setting Config Vars Through Heroku CLI
Open your terminal. Ensure you are logged into Heroku CLI with
heroku login
.Set a Config Var. Use the following command to set a Config Var for your database connection string:
heroku config:set DATABASE_URL="your_database_connection_string" --app <your-heroku-app-name>
Replace your_database_connection_string
with your actual database connection string and <your-heroku-app-name>
with the name of your Heroku app. For example:
heroku config:set DATABASE_URL="mongodb+srv://user:password@cluster.mongodb.net/myFirstDatabase?retryWrites=true&w=majority" --app enigmatic-ravine-23031
Setting Config Vars Through Heroku Dashboard : Easy:
Log in to your Heroku account and navigate to your application's dashboard.
Go to the "Settings" tab and find the "Config Vars" section.
Click on "Reveal Config Vars" to manage your environment variables.
Add a new Config Var by entering
DATABASE_URL
in the key field and your database connection string in the value field. Then, click on the "Add" button to save the Config Var.
6. Monitor Your Deployment
To check the status of your deployment, you can use either the Heroku CLI or the Heroku Dashboard:
Heroku CLI: Run
heroku logs --tail --app <your-heroku-app-name>
to see live logs.
Heroku Dashboard: Log in, navigate to your app, and click on "Activity" for a deployment history.
7. Testing Your Deployed Application
In Postman, replace localhost:port
with your app's URL. a register endpoint might look like:
https://enigmatic-ravine-23031.herokuapp.com/api/auth/register
8. Setting Up Postman Environment Variables
To streamline testing, use Postman’s environment variables:
Open the Manage Environments settings (gear icon).
Click "Add" to create a new environment, e.g., "Production".
Add a variable named
baseUrl
with your Heroku app's base URL as both the initial and current value.Use
{{baseUrl}}
in your request URLs, selecting the appropriate environment from Postman's dropdown.Resources
For a visual guide and more in-depth explanation, you might find this tutorial helpful.
Note: Ensure your application is prepared for Heroku deployment by specifying a start
script in your package.json
and using the process.env.PORT
variable to set the port. This will allow Heroku to correctly start your app.