Deploy - Node/MongoDB
Objectives
Deploy a MEAN app to a production server using Heroku
Use the mLab addon and connect to a mLab instance
First, let's deploy the Node app to Heroku.
Deploying the app
Create a
Procfile
in the root of your Node applicationIn terminal, run
touch Procfile
. Must be called with a capitol Pmake sure it is named "Procfile" (no extention)
make sure your Procfile is in the same folder as your index.js file
in terminal type
echo "web: node index.js" >> Procfile
In your
index.js
file, where you get your server started, include the port number in your app.listen function. Example:
This ensures that when we set the PORT config variable, Heroku will run on it instead of the 3000 port (Heroku automatically includes a port that's public-facing).
Add in your Node version
Note that you may need to specify the node version you're using. To find out what version of node you're using, type the following on your terminal (from anywhere):
Then, put the following into your package.json
file:
Remove the Heroku-Postbuild script if you are launching a decoupled app.
Your package.json file is crucial - when you deploy your application, Heroku will check the package.json file for all dependencies so be mindful to install any dependencies you may have installed globally. You can always check your package.json to see if you are missing anything.
Before you create your app in Heroku, be sure your project is being tracked via a git repository.
Create a Heroku app via the command line
Where sitename
is the name of your app. This will create a url like: http://sitename.herokuapp.com
Commit and push all your data at this point (
git push
).To push to Heroku, enter the following command
In terminal after you deploy your app, type in
heroku ps:scale web=1
this will scale a dyno up
Connecting to a MongoDB Database
Before connecting the Node app to a MongoDB database, we'll need to install an addon called mLab. mLab is a cloud-hosted MongoDB database service that we can connect to.
In order to add the instance, you can use the Heroku toolbelt by typing this command:
This will create a free database instance, limited to 496 MB in storage. Keep this in mind.
Tip: While sandbox mode is a free service, you will unfortunately need to add a credit card to your Heroku account in order to take advantage of this. Please let your instructor know if this is a hardship for you, and alternative grading arrangements will be made.
Configuring Mongoose
After creating the mLab addon, you'll be able to access an environment variable called MONGODB_URI
. You'll want to connect to this URI on production. Alter your Mongoose connection to read this environment variable, but use the localhost string if the variable doesn't exist. Below is an example.
Connecting to a mLab instance using Robo 3T (or similar)
In order to view the contents of your MongoDB database using a GUI client like Robo 3T, you can use the MONGODB_URI
. This URI contains the connection information and credentials needed to connect. You can grab the value of this connection string by typing heroku config
or by logging into the Heroku dashboard.
Last updated
Was this helpful?