{ Introduction to Heroku. }


By the end of this chapter, you should be able to:

  • Deploy simple applications to Heroku
  • Add environment variables to Heroku

Deploying Intro

Now that you have built quite a few applications, it's time to share them with other people! To deploy our applications so that they're publicly visible on the internet, we will be using Heroku. To get started, install the Heroku CLI (toolbelt) here. Make sure you also sign up for an account at heroku.com.

Let's get started with a simple application:

mkdir deploy_me && cd deploy_me
touch app.js
npm init -y
npm install express mongoose
echo node_modules > .gitignore
git init
git add .
git commit -m "initial commit"

In our app.js

var express = require("express")
var app = express()
var methodOverride = require("method-override")
var morgan = require("morgan")
var bodyParser = require("body-parser");

app.set("view engine", "pug");
app.use(express.static(__dirname + "/public"));

app.get("/", function(req, res, next){

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  var err = new Error('Not Found');
  err.status = 404;

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
  app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
      message: err.message,
      error: err

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: {}

// lets see if we have an environment variable for our port (heroku will make one for us so don't worry there, we just don't want to be on 3000)
app.listen(process.env.PORT || 3000);

Once you have signed up and installed an account make sure you are in the terminal and type in heroku login and enter your credentials.

To create a heroku application simply type heroku create NAME_OF_YOUR_APPLICATION (give it a good name!)

The next file we need to create is called a Procfile. To do so we must make sure this is in the root directory and then run the following in terminal

echo web: node app.js > Procfile
git add .
git commit -m "adding Procfile"

Now we can deploy our application using git push heroku master! Once we are done you can type heroku ps:scale web=1 (to start a process for your web application) and then heroku open and you should see your application live!

Environment Variables

To store environment variables we can use the heroku config:set NAME_OF_VARIABLE=VALUE and then access that variable in our node application using process.env.NAME_OF_VARIABLE. Environment variables are essential for configuring an application for production (we will see an example of this in the next section as well). Here is what the terminal command looks like to add an environment variable:

heroku config:set DEBUG=false -a NAME_OF_APPLICATION

Sample App

You can find a very simple Heroku application here.

When you're ready, move on to Adding a Mongo Database to Heroku