Getting Started with Node.js on Heroku on a Windows machine

Heroku is a cloud platform as a service supporting several programming languages which lets app developers spend their time on their application code, not managing servers, deployment, ongoing operations, or scaling…

If you tried to get your feet wet with Heroku by deploying Node.js application, you must have come across this article on Heroku’s documentation (and you also may have run into problems when starting Foreman if you’re on a Windows machine – read onward to see how I’ve solved it).

Obviously, first you have to make an account on Heroku, then install Heroku toolbelt (it gives you git, foreman, and heroku command line interface) for your environment (in my case Windows):
herokuToolbelt

Fire up your command prompt (I use Console2) and run heroku login . You need to have Node.js installed (in case you don’t you can download it here).

Write some Node.js app and put it in app.js file:

//app.js
var express = require("express");
var app = express();

app.get('/', function(req, res) {
  res.send('Every day in every way I\'m serving more requests');
});

var port = Number(process.env.PORT || 5000);
app.listen(port, function() {
  console.log("Listening on " + port);
});

If you have  package.json  file then Heroku will recognize your app as Node.js app. In order to create it, run npm init  in the root directory of your app. The npm init utility will walk you through creating a package.json file by asking few questions. If you already made your repository on GitHub and cloned it locally then npm init command in this folder will recognize that too and add:

"repository": {
    "type": "git",
    "url": "https://github.com/yourUsername/appName.git"
}

If you wish, you can freely clone my test app from Github: https://github.com/Hitman666/herokuTestApp.git

Next, install dependencies from your code (in app.js  example the required module is express). Use npm install <pkg> –save  to install a package and save it as a dependency in the package.json file. In my example that would be one command:

npm install express --save

 

You have to set a Procfile, which is a text file in the root directory of your application, to explicitly declare what command should be executed to start a web dyno. In our case this would be the contents of Procfile:

web: node app.js

Now you should be able to start your application locally using Foreman (installed as part of the Heroku Toolbelt) by running foreman start .

But here the party started :/

In my case foreman didn’t start at all, and after a lot of searching, I managed to solve it in few steps. First, I updated my Ruby installation:

gem update --system --source http://rubygems.org/

Then, according to this StackOverflow post I installed former version of foreman:

gem uninstall foreman
gem install foreman -v 0.61

Finally, I added Ruby’s bin folder (Ruby which, in my case, is in C:\Program Files (x86)\Heroku\ruby-1.9.2\bin) to my PATH variable.

Ok, almost there! Lets add our app to git (you should skip the git init command if you already cloned your app from GitHub):

git init
git add .
git commit -m "init"

and finally, lets deploy it to Heroku:

heroku create
git push heroku master

To open the app in your browser run heroku open . To push the changes to GitHub you have to execute git push origin master.

*Now that your app is up and running on heroku (my link) you may want to prevent it from going to sleep mode. Some suggestions on how to achieve this are in this post on StackOverflow (I went with UptimeRobot).

**At some point, as I was fiddling with the apps on Heroku’s website, I deleted all apps and then the git push heroku master  command from my console was not working anymore, so I had to do:

git remote rm heroku
heroku create
git push heroku master

***Also, I wanted to rename my app and that can be done easily:

heroku apps:rename NEWNAME

 

Written by Nikola Brežnjak