Setting the port for node.js server on Heroku
Asked Answered
C

9

76

I launched a node.js server with the following line to set the port:

app.set('port', process.env.PORT || 8080);

This means that, it should either read the PORT env variable or default to 8080, as it does when it's run locally. Neither of them is happening on Heroku, and the server always uses the default port 80. Any idea how to change it?

heroku config
PORT: 8080
Coextend answered 24/2, 2015 at 21:2 Comment(3)
what does heroku config:get PORTgives you ? Also, you can try heroku config:unset PORTand see if your hardcoded default gets picked up.Dede
'heroku config:get PORT' gives me 8080, yet the app is served on 80. After 'heroku config:unset' no changeCoextend
Hey you accepted an answer, yet it doesn't really look like an answer, in my case process.env.PORT is undefined on heroku and I'm kinda stuck here.Delainedelainey
Z
105

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

Zeal answered 24/2, 2015 at 21:57 Comment(2)
So, in the app.js (or the start script), what do I use for port? var server = http.createServer(sendSMSHandler); server.listen(PORT);Jeremie
@broderickga You can use something like this:var port = process.env.PORT || 3000; app.listen(port, function() {Greig
D
40

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

Dorman answered 14/10, 2018 at 19:21 Comment(2)
This solution worked for me.Alexine
I don't understand. Your answer is the question itself...Dysphoria
T
21

All the answers are great! Wanted to touch on the theory a bit so people can understand why Heroku sets it port to 80 or 443.

Computers had to create a convention to communicate with each other with a different protocol. For example HTTP, HTTPS, SSH, FTP , etc.

As a result, there was an agreement that computers will communicate HTTP with port 80, https will communicate on port 443, and so on with the other protocol. Below is a table displaying all the protocol's conventional reserve port number.

enter image description here

Now some of you people may be thinking well if these are reserve port number how come my computer lets me use port number 80 and 443 (ex localhost:80). You can use any port number (in fact you can choose up to 65,535 port number) but once you want to deploy to live and want others to use your application then you will have to start using the convention of port 80 (HTTP) or port 443 (https).

Heroku makes it nice and easy for you by providing an environment variable process.env.PORT to apply for the correct conventional port number so others can access your app.

Tun answered 20/8, 2020 at 12:48 Comment(0)
U
12

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

Unhallow answered 28/7, 2018 at 14:23 Comment(1)
This works. If desired it's also possible to see the assigned port in the log and the app is accessible via the same.Script
O
8

You can do for example:

const PORT = process.env.PORT || 5001;

app.listen(PORT, () => console.log(`Server is listening on port ${PORT}...`));
Overmatter answered 24/9, 2019 at 17:8 Comment(0)
K
4

In my case, heroku was listening on the default HTTPS port: 443 and it was not visible via heroku config:get PORT.

Kloof answered 15/1, 2019 at 14:34 Comment(1)
Same here. I set the Config Vars to port 5000 and It took me 4 hours to figure out that it didn't care. It was using ports 80 (for HTTP) and 443 (for HTTPS) no matter what. I actually found this post after finding out and see if there was any documentation at all.Bade
S
2

80 is the default port so use 80 instead of 3000 or 8000

const PORT = process.env.PORT || 80;
var server = app.listen(PORT, function() {
    var host = server.address().address;
    var port = server.address().port;
    console.log("server is listening at http://%s:%s", host, port);
});
Scurry answered 18/12, 2020 at 19:13 Comment(0)
I
0

Different ports on Heroku can be used, as long as the main $PORT is also used. See https://mcmap.net/q/266774/-how-to-run-django-and-node-js-app-on-same-dyno-in-heroku

Indochina answered 28/6, 2022 at 22:53 Comment(0)
M
0

For modules solution:

const process = require('process');
const port = process.env.PORT || 8080;
Miniaturist answered 28/2 at 11:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.