Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to listen on any port on heroku when a web port 80 is already in use?

I am new to heroku and i have deployed a meteorjs application on heroku. My Meteorjs application is a webapplication, so after the build is done it runs on heroku on port 80. But simultaneously i also want to listen on a port eg:4000 or any so that i can catch my webhook events fired by any third party i want to listen to. On my local machine it runs perfectly with webapp running on a port and listener running on another, but on heroku after i deploy its just the webapp which runs and listener doesn't listen. Can anybody help me or guide me. Any new solutions are also welcome..

In my server/main.js i have made a bot instance of facebook-bot-messenger and below is the code to start the listener

bot.listen(4000);

In my client/main.html i have the html code which is just hello world

On local machine when i visit http://localhost:3000 i can see helloworld and app is also listening on port 4000.

On heroku when i visit https://appname.herokuapp.com i see helloworld . In this case the port is 80 which is fine, but the bot is not listening as any post calls i make on https://appname.heroku.com on port 4000 doesn't respond.

like image 213
Chetan Avatar asked May 12 '17 19:05

Chetan


People also ask

What port does heroku listen on?

Heroku expects a web application to bind its HTTP server to the port defined by the $PORT environment variable. Many frameworks default to port 8080, but can be configured to use an environment variable instead.

Can heroku use localhost?

Start your app locally using the heroku local command, which is installed as part of the Heroku CLI. Your app should now be running on http://localhost:5000/.


Video Answer


1 Answers

Heroku assigns the port for you, so you can't set your own. You will have to read in the port number from the PORT environment variable. In Node this would be process.env.PORT

You can also use || for as and OR. For example in the below case, it will read in the port number from the environment variable while on Heroku, but locally it will default to port 4000.

const app = express();
app.set('port', (process.env.PORT || 4000));

//Start Server
app.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));
});
like image 51
Alek Avatar answered Oct 20 '22 20:10

Alek