Skip to content

5: Deploying to Heroku

Teodor Elstad edited this page Feb 17, 2016 · 3 revisions

Heroku is a cloud platform as a service, enabling you to deploy your web application through git and build scripts. It supports node and therefore npm, allowing easy deployment of the web server.


Setup

Navigate to the 'every-bit-matters' folder. Type:

    $ git checkout -f stage-5 

Nice. You have now checked out the code required to start. Let's begin.

Deploying to Heroku

To deploy the application to Heroku, you must have an account through Heroku and the Heroku Toolbelt.

To get started, we need to authenticate you with heroku:

    $ heroku login 

Login with your username and password, and create your application:

    $ heroku create

Great job! When it's finished, it should tell you something like:

    Creating app... done, stack is crazy-stack
    https://some-crazy-url.herokuapp.com/ | https://git.heroku.com/some-crazy-url.git

Copy the URL, and paste it into your browser. Have a look at your beautiful app.

But wait! It's not the app we have created. Yet.

Before we can push it into the cloud, we need give Heroku two things:

  1. The filename which bootstraps our server (server.js)
  2. Access to which port server.js runs on

Procfile

A Procfile is a mechanism for declaring what commands are run by your application’s dynos on the Heroku platform.

Create a "Procfile" in the root of the repo (every-bit-matters). Enter the following:

     web: node server.js
 

This code tells Heroku which process to run to start our server.

Port Number in 'server.js'

We now need to configure which port Heroku runs our server on. As we need to let Heroku control this, we use the 'process.env' – short for process environment – enabling us to use the variable PORT exposed by Heroku.

The following code will do the trick:

...
    var port = process.env.PORT || 3000;
...

...
    app.listen(port, function callback () {
        console.log('Running our app at http://localhost:3000')
    });
...

Great job!

Push it to the cloud

Now we only need to add our files and commit!

   $ git add .
   $ git commit -m "Ready to push for Heroku."

Now we can push our app into the cloud (push our branch into Heroku's master which is then deployed):

   $ git push heroku stage-5:master

Voila. Now you can visit your app on: http://[your-crazy-url].herokuapp.com

Configure the server from the heroku admin panel

We could also control the logging interval from the heroku admin panel. This will be demonstrated in the workshop, and implementation is left as a exercise to for the reader. Have a look at this guide to get started.

In addition, the logger has to be configured with the URL to your heroku application. This can be done by updating logger.js, but could you get this value in any other way?

If you get stuck, just run git checkout -f finished to get a finished version.