Node.js app in Heroku Works on Foreman but not When Deployed?

Problem

I successfully have my app running on foreman start perfectly well, but as soon as I deploy it to Heroku, it throws me an error in the logs:

2012-08-20T03:22:48+00:00 heroku[web.1]: Starting process with command `node index.js`
2012-08-20T03:22:49+00:00 app[web.1]: Server has started.
2012-08-20T03:22:49+00:00 heroku[web.1]: Error R11 (Bad bind) -> Process bound to port 15134, should be 52717 (see environment variable PORT)
2012-08-20T03:22:49+00:00 heroku[web.1]: Stopping process with SIGKILL
2012-08-20T03:22:51+00:00 heroku[web.1]: Process exited with status 137
2012-08-20T03:22:51+00:00 heroku[web.1]: State changed from starting to crashed

I have tried to change which port it binds to, but every time I do it changes the port again. Is there something else wrong in my app?

Problem courtesy of: Brad Ross

Solution

on Heroku, you need to use the port contained in an environment variable: process.env.PORT

Solution courtesy of: Rotem Harel

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.