2

I made a small app using node.js and Heroku, set the local .env file and also used heroku config:set VARNAME=varValue to define the port it should listen to both locally and remotely.

PROD_MONGODB=mongodb://xxxxxxxxx@dxxxxxxxx:12345/server
PORT=3000

I've also checked the app's dashboard on Heroku and I can confirm the vars are set correctly.

Here's the server startup code using restify.

app.listen(process.env.PORT, function() {
    console.log('Server listening on port number', process.env.PORT);
});

If I run the app locally using heroku local, it runs on port 3000 as I expected:

Orion:.......$ heroku local
forego | starting web.1 on port 3000
web.1  | 
web.1  | Server listening on port number 3000

But when I deploy the app on Heroku, I uses a seemingly random port:

2015-09-28T15:15:51.400324+00:00 app[web.1]: Server listening on port number 56680
2015-09-28T15:15:51.486396+00:00 heroku[web.1]: State changed from starting to up

Is there anything else I can do to make sure Heroku uses the port I defined as an environment variable?

nacho_dh
  • 1,847
  • 3
  • 18
  • 27

1 Answers1

3

You don't define PORT - Heroku automatically provides that as part of the environment.

hunterloftis
  • 13,386
  • 5
  • 48
  • 50
  • I didn't understand that part, thank you. Also, according to this other q&a, Heroku asigns whatever port it wants, but the app is always served on port 80. http://stackoverflow.com/questions/28706180/setting-the-port-for-node-js-server-on-heroku – nacho_dh Sep 28 '15 at 16:12
  • Yes, externally the app will be served on 80 (or 443 for HTTPS). Internally it will listen on the assigned `PORT`. – hunterloftis Sep 28 '15 at 17:42