18

I'm running a basic Express app in Node.js and trying to deploy to Heroku. The app works fine locally and I believe my setup with Heroku has gone well up until starting the server where i get the following error:

2011-09-21T16:42:36+00:00 heroku[web.1]: State changed from created to starting
2011-09-21T16:42:39+00:00 app[web.1]: Express server listening on port 3000 in production mode
2011-09-21T16:42:40+00:00 heroku[web.1]: Error R11 (Bad bind) -> Process bound to port 3000, should be 12810 (see environment variable PORT)
2011-09-21T16:42:40+00:00 heroku[web.1]: Stopping process with SIGKILL
2011-09-21T16:42:40+00:00 heroku[web.1]: Process exited

this is currently all i have in my app.js

app.listen(3000);

I did also run this as mentioned on Heroku's getting started.

$ heroku config:add NODE_ENV=production
Adding config vars:
NODE_ENV => production

I believe I just need to set up the port for production? Thanks.

tuddy
  • 1,824
  • 4
  • 31
  • 35

1 Answers1

74

Can you show the entire section of code where you call listen? You should be checking for the process environment variable PORT, not just hardcoding it to 3000. From their docs:

var port = process.env.PORT || 3000;
app.listen(port, function() {
Femi
  • 64,273
  • 8
  • 118
  • 148
  • Cool, I updated the post. Do I need to switch to port 12810 for production somehow? Thanks – tuddy Sep 21 '11 at 18:00
  • 1
    Ah, alright. Try calling listen using the logic I showed above, and see if it works. – Femi Sep 21 '11 at 18:06
  • this works, but what does `process.env.PORT` do? Who sets it? – Alexis Dec 03 '12 at 23:54
  • Heroku does: they inject the configuration variables when they start up a web dyno, and that is one of the variables they inject. – Femi Dec 04 '12 at 01:30
  • i can't find PORT variable in Environment variables, need to mention it in Procfile -p command. How can i do this? – rupinderjeet Jul 08 '16 at 18:05