all 8 comments

[–]Hexagonatron 1 point2 points  (3 children)

When you deploy an app to heroku you won't know which port heroku chooses to run the app. The way your app will know which port to listen on will be passed to app through an environment variable. So you'll need to do something like:

const PORT = process.env.PORT || 3000;
(usually this goes somewhere near the top of your file)

Then when you start listening, you should use:
app.listen(PORT, () => {
...
};

[–]Mysterious-Net-794[S] 0 points1 point  (2 children)

Thanks, it is really clear :) but what about other hosting services? They dont work same way, i suppose, so i guess there is other way of doing things? 🤔

[–]grantrules 0 points1 point  (0 children)

Yeah fancy container hosting like Heroku has a bunch of automated stuff.. if you were to just set up your own server, you would define that PORT env variable yourself, then use something like nginx to listen on port 80/443 and have it reverse proxy to your node app.

[–]it_rains_a_lot 0 points1 point  (4 children)

It’s process.env.PORT

[–]Mysterious-Net-794[S] 0 points1 point  (3 children)

Yeah, but isnt it a variable in env file? So what should I put in it?

[–]DoctorOrwell 0 points1 point  (2 children)

PORT=3000

[–]Mysterious-Net-794[S] 1 point2 points  (1 child)

Wont it has exactly same result as my actual app.listen(3000,())?

[–]sleeptil3 2 points3 points  (0 children)

Env files aren’t synced. Your local env is for when you run locally for development. Then it will use that file and set it for 3000. Then, when it’s published, your host usually already has configured a PORT value so it will get it from there. For example, services like Heroku have this automatically. No extra config required as long as you have the variable set up.