Node, Mongo, Heroku Deployment Lab
Use Heroku + mlab to host MongoDB + your Node server on teh interwebz.
Accounts Required
You will need two PaaS (Platform as a service) accounts. You should select the free account models and pricing for anything used in these labs. Free accounts are perfect for testing (just not production). Before continuing...
- Register an MLab (formerly MongoLab) account: https://mlab.com/
- Register for a Heroku account: https://heroku.com
Objectives
- Generate an Express application to serve to the internet.
- Add Mongoose Models and a Database connection to your Express application.
- Use the environment variables with
.env&dotenvmodule to hide important data (such as your connection string) from public eyes. - Use MLab (Database as a Service) to host a production ready MongoDB Database online.
- Use Heroku (Platform as a Service) to deploy a Node application online.
- Create a
Procfileto tell Heroku to run your node app.
Lab
Getting Started
- Install the following modules:
dotenv. - Add and commit everything.
- Create a
.gitignore(or edit an existing). - Inside of the
.gitignore, on a new line add a new file to ignore:.env - Add and commit the gitignore. Do not create the
.envfile yet. We need to add and commit changes to our.gitignorebefore Git will start ignoring things. So we have to tell Git to ignore the.envfile before it begins ignoring it. - Now that you have committed, create the
.envfile. - Read the dotenv module documentation to learn how to include the module in your file. When mentioned to include a call at the start of your project, you can add that call before everything else in
app.js. - In your
.envfile, add a key calledDB_HOSTwith an empty value. Such value may appear as...DB_HOST=. - Add and commit. If you have not added a
remoteto your Git repository yet, you should do so now. Typically you wouldgit remote add name-of-remote git_url_hereto do so.
Hosting MongoDB on mlab and using it
- Browse to https://mlab.com and login.
- Select Create New ...

- Select any Single Node with a Sandbox/Free Tier from your preferred provider. You will not need an account from any of these services; however for production use in the future you could integrate them.

- Name your database something clever or obfuscated if you prefer. Then select Create new MongoDB deployment.
- You'll be returned a list of databases.

- Select your Database.
- You will then be provided a warning that A Database user is required to connect to this database! Click on the link provided to create a new user.
- Create a new user and password for your root database account. This will be the user that can CREATE, READ, UPDATE, and DESTROY data. Protip: You can create a read-only database user to hand out to other developers/data scientists/etc for testing/etc.
- At the top of your page for Database Users, you will see two connection methods that MLab provides. One is a shell connection string (for command line MongoDB access). The other is a URI for connecting with ORMs/ODMs (and other programmatic scripts/tools).
- Copy the URI to your
.envfile where we had an emptyDB_HOST. We now have the appropriate value! We can paste it here. - However... there is a problem. We need to add the MLab user's name and password here. Good thing we hid our
.envwith our.gitignore, amirite? Remove the<user>and<password>and replace them with the username and password for that user. - Add and commit. Notice something funny?
- Now we need to create a
db/db.jsand a Model for a brand new collection. Let's start by making aTacomodel. Createdb/db.jsand a/models/Taco.js. - Inside of your
db/db.js, requiremongooseand then create a variable calledvar connectionString. The value for this can be found usingprocess.envto get appropriate value. If you are unsure (or confused), go back to the npm dotenv documentation and look at how to reference environment variables via keys from the.envfile. - Use Mongoose to connect to your database. You should listen to the
connected,disconnected, anderrorevents. Log them out to verify that you are actually connecting. - Now, let's open that freshly created
models/Taco.jsfile. Yum. Now, require mongoose. - Declare a new Schema for a taco. The taco should have at least a few attributes... toppings, shellType, orderedFrom as Strings...?
- Export your Schema using
mongoose.model. The model's name will beTaco. - Add a persistent database connection to your app using the
db/db.jsfile. Depending on your usage, your mileage and installation needs may vary. - Now, let's test this out - open the
routes/index.jsand create a newgetroute. The route will be/tacos. - Require your model -
var Taco = require('../models/Taco'); - In your
/tacosGET route, use theTacomodel to get all tacos and send them back to the client using Express Router'sresponse.json()method. - Save your changes.
- Run
npm start(ornodemon) to run your server. - Browse to
localhost:3000/tacos- what do you see? If you don't see an empty[]of 0 objects, something went wrong. Otherwise, good job!
Heroku Deployment
- Heroku will allow a user to deploy 5 free proejcts. Log in.
- Download/install the Heroku toolbelt.
- You may need to restart your terminal session - just in case - before moving forward. Why? Because we just installed software to modify your terminal's environment and we need those changes.
- We'll use the Heroku command line interface (CLI) to create an app on heroku for us. Let's do that now.
heroku create your-app-name-here- you should make sure your app name is unique and name it after something you would want to be public. For example if we did the prior, we could access it via https://your-app-name-here.herokuapp.com- Once this is done, add and commit everything.
- Head over to your trusty
.envfile. Keep it open. - Browse back to Heroku. Open your application - you should see it listed on your Dashboard!
- Select Settings on the top-right corner.
- Scroll down to Config Variables and reveal them. You'll now need to transfer the contents of your
.envto this area. This is a secret, safe place to store environment variables for your app on Heroku. - Because our
.envnever gets pushed, we have to add them physically somewhere. The Config Variables in Heroku is that place. Other platforms may have similar abilities. - Finally, we need to add Node support to our Heroku project.
- In our repository, create a new file:
Procfile(no extensions). - Inside of this file, we need to tell Heroku that this is a Node app. By default, it will likely assume we're writing in Ruby. That's no beuno. The
Procfileshould contain the following content:web: npm start - This tells
Herokuto usenodeto call ourapp.js... which we declare in package.json'sstartscript fornpm start. - Add and commit.
- Then
git push origin masteras well asgit push heroku master. - Once done, you may call
heroku openin the command line. - Your app should be up and running!
- Browse to your
/tacosroute. What do you see? - Congrats - you've now deployed live! Good job.
Further Considerations
- Companies like General Assembly using PaaS solutions to deploy quickly.
- Why might this be awesome? What advantages do you see to using PaaS vs building your own virtual servers?
- Describe how the
.envfile works with the.gitignore. - Explain why we need to use Heroku's Config Variables because of this.
- Who might you trust with all of your secret datas? Co-workers? Friends? Your grandma?