Setting the port for node.js server on Heroku Setting the port for node.js server on Heroku express express

Setting the port for node.js server on Heroku


You can't. Heroku sets the PORT variable that you are supposed to bind, and listens on tcp/80.


You should use the port opened by heroku like so:

port = process.env.PORT || 80

It sets the port to 80 if it has somehow not been set already


Heroku treats web apps just like any other app and doesn't allow you to assign listening ports directly. Your web server will be assigned a dynamic port by Heroku but to ACCESS it, you will need to use the default port (80).

On Heroku, apps are completely self-contained and do not rely on runtime injection of a webserver into the execution environment to create a web-facing service. Each web process simply binds to a port, and listens for requests coming in on that port. The port to bind to is assigned by Heroku as the PORT environment variable.

The contract with Heroku is for the process to bind to a port to serve requests. Heroku’s routers are then responsible for directing HTTP requests to the process on the right port.

Reference: Heroku Runtime Principles - Web Servers