Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed Deploy a Node Application to Heroku!
      
    
You have completed Deploy a Node Application to Heroku!
Preview
    
      
  Now you know what Heroku is, let's get our application ready and deploy it!
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
                      Now you have
the Heroku Toolbelt installed,
                      0:00
                    
                    
                      it's now time to deploy our application.
                      0:03
                    
                    
                      Let me show you what the application is.
                      0:06
                    
                    
                      It's a simple HTTP server, but
                      0:11
                    
                    
                      just says Hello World no
matter where you navigate to.
                      0:13
                    
                    
                      Heroku users get to deploy applications.
                      0:20
                    
                    
                      So if you haven't got your
application checked since the git,
                      0:24
                    
                    
                      which you should have by now,
you need to do that.
                      0:27
                    
                    
                      My application is already committed.
                      0:30
                    
                    
                      Just committing your application to
git is not enough to deploy to Heroku.
                      0:37
                    
                    
                      There's a couple of things you need to
do to prepare your app for deployment.
                      0:42
                    
                    
                      Make sure you have a package.json file.
                      0:47
                    
                    
                      Because Heroku doesn't know which version
of Node you've been developing for,
                      0:50
                    
                    
                      you need to specify what version of
Node.js that Heroku will install and
                      0:54
                    
                    
                      use for your application.
                      0:59
                    
                    
                      You need to have a special file
at the root of your application
                      1:01
                    
                    
                      that will let Heroku know
how to run your application.
                      1:05
                    
                    
                      I'll go into more detail
about this file shortly.
                      1:09
                    
                    
                      Then create the application on
Heroku using the Heroku Toolbelt.
                      1:12
                    
                    
                      This will link your Heroku application
as a remote git repository too.
                      1:16
                    
                    
                      Each Heroku application is stored in
virtual machine containers called dinos.
                      1:21
                    
                    
                      Each dino has its own
configuration settings, for
                      1:26
                    
                    
                      example, the port number that
it routes web traffic to.
                      1:29
                    
                    
                      There's a way you can
programmatically access this, and
                      1:32
                    
                    
                      I'll show you that in a minute.
                      1:35
                    
                    
                      Let's first create a package.json file.
                      1:37
                    
                    
                      We do this by using npm init.
                      1:40
                    
                    
                      A package.json file helps manage
dependencies for your project,
                      1:45
                    
                    
                      whether if it's an application or
an npm package.
                      1:50
                    
                    
                      Now there's no dependencies for
this application.
                      2:02
                    
                    
                      But there is one dependency that you don't
normally see in a package.json file,
                      2:05
                    
                    
                      unless you're deploying it to Heroku,
and that's engines.
                      2:09
                    
                    
                      The engines key is to tell Heroku
which version of node.js to use when
                      2:24
                    
                    
                      running our application.
                      2:29
                    
                    
                      As of this recording,
the latest long term support version of
                      2:31
                    
                    
                      node.js is 4.2.4, so let's use that.
                      2:36
                    
                    
                      Next we need to tell Heroku how
we need to run our application.
                      2:51
                    
                    
                      We run our application with node app.js.
                      2:55
                    
                    
                      Heroku uses a special file called
a procfile or a process file.
                      3:01
                    
                    
                      A process is a running application.
                      3:08
                    
                    
                      You can tell Heroku any
number of processes.
                      3:12
                    
                    
                      In this case, you'll need at least one for
the web server.
                      3:14
                    
                    
                      You first start with the name of
your process followed by a colon,
                      3:19
                    
                    
                      then the command you
want the process to run.
                      3:24
                    
                    
                      Let's commit our changes.
                      3:32
                    
                    
                      Next, let's create
the application on Heroku.
                      3:48
                    
                    
                      You'll need to log into Heroku if you
haven't already with Heroku log in.
                      3:52
                    
                    
                      If you haven't used it before,
you may see a screen like this.
                      4:01
                    
                    
                      Don't worry.
                      4:05
                    
                    
                      Fill in your credentials,
and you should be logged in.
                      4:06
                    
                    
                      Heroku Toolbelt is a command line
utility to help you manage your
                      4:16
                    
                    
                      applications using commands.
                      4:21
                    
                    
                      Type heroku help to see
a list of command topics.
                      4:23
                    
                    
                      See, there's an apps topic,
used to manage applications, create and
                      4:34
                    
                    
                      destroy them, for example.
                      4:38
                    
                    
                      To see a list of commands in that
topic area, type heroku help apps.
                      4:40
                    
                    
                      The first command is Heroku apps:create,
with an optional application name.
                      4:47
                    
                    
                      If you don't include a name,
they'll generate one for you.
                      4:56
                    
                    
                      The application name translates to a
subdomain to access your replication app.
                      5:00
                    
                    
                      If you don't want something random,
choose something yourself.
                      5:05
                    
                    
                      I choose helloworld-treehouse.
                      5:13
                    
                    
                      Since I picked this name, no one else can.
                      5:20
                    
                    
                      During the application creation process,
                      5:23
                    
                    
                      Heroku adds a new remote repository to
your git repository on your local machine.
                      5:25
                    
                    
                      An example of another remote
repository is a GitHub repository.
                      5:31
                    
                    
                      This is typically called origin.
                      5:36
                    
                    
                      When you do git push origin master,
it pushes everything from
                      5:38
                    
                    
                      your master branch on your local machine
to the master branch on GitHub the origin.
                      5:44
                    
                    
                      With Heroku,
it creates a remote called heroku.
                      5:50
                    
                    
                      So to deploy to Heroku,
you can simply do git push, heroku master.
                      5:54
                    
                    
                      But what you want to do before deploying
to Heroku is test that it runs locally.
                      6:02
                    
                    
                      To do this,
you use the heroku local command.
                      6:11
                    
                    
                      If you notice,
it says it's running on port 5000.
                      6:19
                    
                    
                      However, our application
runs on port 1337.
                      6:22
                    
                    
                      There is a disconnect.
                      6:26
                    
                    
                      If you try and visit 5000,
it's not there, but 1337 is.
                      6:30
                    
                    
                      Heroku expects that your application
coding will handle an environmental
                      6:38
                    
                    
                      variable called port.
                      6:42
                    
                    
                      An environment variable is a variable that
can be accessed by the process and used.
                      6:44
                    
                    
                      Heroku local sets up the port
environment variable
                      6:49
                    
                    
                      just like it does in the Heroku
live hosted environment.
                      6:52
                    
                    
                      Just simply put process.env.port
                      6:59
                    
                    
                      followed by the or operator and
                      7:04
                    
                    
                      then the default port of your choosing.
                      7:08
                    
                    
                      What this code does is it sets the port
to whatever the environment variable is.
                      7:14
                    
                    
                      If there isn't one,
it will set it to 1337.
                      7:20
                    
                    
                      If we terminate the process with Ctrl+C,
when we do heroku
                      7:25
                    
                    
                      local now, it correctly runs at port 5000.
                      7:31
                    
                    
                      If we kill this process
again with Ctrl+C and
                      7:37
                    
                    
                      directly run the app with node app.js,
it will run
                      7:41
                    
                    
                      on port 1337, since we're not running it
in an environment where the port is set.
                      7:47
                    
                    
                      Finally, we can commit our changes.
                      7:53
                    
                    
                      We can push them using
git push heroku master.
                      8:09
                    
                    
                      It's now deployed on Heroku.
                      8:21
                    
                    
                      If your code is on GitHub,
                      8:23
                    
                    
                      you probably want to deal with
git push origin master right now.
                      8:25
                    
                    
                      We can now visit our
applicationname.herokuapp.com or
                      8:29
                    
                    
                      type heroku open to open
it up in a web browser.
                      8:34
                    
                    
                      No!
                      8:41
                    
                    
                      There is an error with our application,
and this is a common gotcha.
                      8:41
                    
                    
                      It may work on your local environment,
but it may not work on Heroku.
                      8:46
                    
                    
                      And the reason is this.
                      8:50
                    
                    
                      Now, the reason it doesn't work on Heroku,
but
                      8:52
                    
                    
                      it does on our local machine is
because we're using a hostname here.
                      8:55
                    
                    
                      So let's just remove these because
hostname is actually optional, so
                      8:59
                    
                    
                      it will still work on our local machine,
and in theory, it should work on Heroku.
                      9:04
                    
                    
                      So let's save our file.
                      9:10
                    
                    
                      And do git status, and
we've only modified our app so
                      9:14
                    
                    
                      let's do git add, git commit -m "Removed
                      9:20
                    
                    
                      hostname", git push heroku master.
                      9:26
                    
                    
                      When we do heroku open now, it works.
                      9:37
                    
                    
                      Cool.
                      9:40
                    
                    
                      And that's it.
                      9:41
                    
                    
                      For further reading,
                      9:42
                    
                    
                      check out Getting Started with
Node.js on Heroku's Dev Center.
                      9:43
                    
              
        You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up