Dynamic port & Bower + Grunt scripts with Heroku host

I use Heroku to host some project like Osprey, and I need to admit that this is really a nice platform to host your personal projects for free. It’s really easy to start with but you need to know a few of things…

First of all, check the Node tutorial provided by Heroku.

Bower & Grunt

If you have some bower or grunt scripts to launch after the npm install and before the npm start, you need to add a bash script reference in your package.json file. Your package.json file will contain a postinstall command:

"scripts": {
  "test": "echo \"Error: no test specified\" && exit 1",
  "start": "node server.js",
  "postinstall": "bash ./build.sh"
},

And your build.sh file should contain all the commands to build the rest of your project (copy, move files, etc…)

#!/bin/bash
./node_modules/bower/bin/bower install
./node_modules/grunt-cli/bin/grunt

Dynamic port

Heroku assigns dynamically a port to your app. So, if you have set a fixed port in your server.js file, it will not work with the heroku deployment. The port is added to the process.env.PORT variable so you need to pull it from there. You app.listen method should look something like this:

var port = process.env.PORT;
app.listen(port || 3000);

Leave a Reply

Your email address will not be published.