Putting Node Express app on Heroku

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

In the last tutorial, we created an simple Hello WorldNode Express app which we will not deploy to Heroku for hosting on the public internet.

Step 1:

Login to your Heroku hosting account and click create new app…

new heroku app

Step 2:

Since we have not purchased a domain name for our app, we will create a sub-domain name on Heroku’s domain.  Come up with an unique sub-domain name for the app and enter it as shown below.  Or leave blank, and Heroku will make one up for you.

Enter app name

Step 3:

After clicking the Create App button (shown above), you get to the deploy instructions page that looks like this…

Heroku deploy instructions

 

Step 4:

Downloading and install the Heroku CLI (formerly known as Heroku toolbelt) as mentioned.  Then you should be able to type “heroku login” in your terminal inside your project folder.  You may need to restart terminal after installing the CLI.  CLI stands for command line interface.

It will ask for your Heroku login email and password.

Step 5:

Use or generate a SSH key on your machine and paste the “public key” into your Heroku account as described here.

Step 6:

You get files up to Heroku using Git   Install it if you don’t already have.

Step 7:

In terminal, navigate to project folder.  Ours is “c:/temp/hello” from last tutorial and type “git init” to initialize a new Git repository for this project.

Step 8:

Since Heroku will recreate the node_modules folder based on your package.json file, we do not need to push the “node_modules” folder up to Heroku.  Therefore, create a .gitignore file in the project root with the following line…

gitignore file

Also add a start script to your package.json, so that Heroku knows how to start up your app…

add start script

Another change we need to make on our app is …

set port

The process.env.PORT is actually important here.  This is the environment variable on Heroku that contains the port number.  When on the Heroku cloud, the app will listen on this port.  But on local machine, it will listen on port 3000.

Step 9:

Now when you run git status, you see only these files (without the node_modules)…

git status

Step 10:

Add the files to stage with the command …

git add .

git add

Step 11:

Run git commit …

git commit

Step 12:

To set up a Git remote to Heroku, run the command …

heroku git:remote -a your-app-name

And you should see the heroku remote when running ...

git remove -v

Step 13:

Now you can push to heroku with …

git push heroku master

Step 14:

Type “heroku open” and it will open a new browser to your app.

If you need to restart your app, you can do in Heroku dashboard …

restart app

Also if you need to shut down your app or delete your app…

shut-down-app