Hosting a Node app on Heroku

Note: I apologise for the lack of updates on JS Playground recently, but happy to announce the site is now returning to at least one post per week. Any requests for content, please get in touch.

Today I want to look at using the popular Heroku to host a simple Node app. I got asked recently if I had any suggestions on hosting a small Node app, and Heroku's free plan is usually more than enough for little side projects, or to show off something you're working on. It can be a bit daunting if you've never used it before, so I thought a step by step guide would be of use. This tutorial does require knowledge of Git and also you should be comfortable with the command line - if you're not then Heroku probably isn't for you.

Heroku is heavily used to run Ruby / Rails apps but recently added Node.js support and it's a really great way to quickly and easily get something running online.

If you haven't already, you'll need to sign up to Heroku, which is completely free. You'll then need to install the Heroku Toolbelt, which will give you access to the heroku command line interface.

For the app, I'm going to use the small Express server example I introduced in my Beginning Node tutorial. This contains helloworld.js, which has the following:

We need to make one change to this though. Heroku will need us to run on a specific port, which we access through the process object, which is available to use. Make your app listen on the port number specified in process.env.PORT, or if it can't find one, revert to 3000. This way it will work both locally and on Heroku.

This is using an outdated version of Express but for this tutorial it's irrelevant - there's tutorials planned around Express V3 in the near future.

It's important to note that you have to be using NPM to manage your dependencies to host with Heroku. You also need to be using Git as your VCS too, as to update files on Heroku you do a git push. Run npm install to make sure your package.json file is valid, and that you've got all your dependencies sorted.

Next we need to tell the Heroku server how it should run our app. This is done through what Heroku call a Procfile. It's a simple text file created in the project root and for this example, we simply need to tell it how to run our app, which is done like so:

web: node helloworld.js

That's all your Procfile should contain. Youc an test this by running it through Foreman, a way of running apps that uses a Procile to do it. Run foreman start (it's installed as part of the Heroku toolbelt) and you should see output somewhat like this:

If you get that, everything's working fine. You can go to localhost:3000 to check if you want to make sure.

Now it's time to get these files into Git. Initialise your Git repository if your code is not in Git already, and commit all the changes we've made. Now we're ready to run it on Heroku.

On the command line, run heroku login. This will authenticate you and set up any neccessary public keys required to allow you to push to Heroku. Now run heroku create to get Heroku to set up a site for you: