I have my first node.js app (runs fine locally) - but I am unable to deploy it via heroku (first time w/ heroku as well). The code is below. SO doesn\'t let me write so much
From the heroku bash process, pass down the value of $PORT to your node app using an options parser like yargs.
Here is an example of how you might do that. On the scripts object, inside package.json, add a start method "node server --port $PORT".
In your server file, use yargs to get the value from the port option (--port $PORT) of the start method:
const argv = require('yargs').argv;
const app = require('express')();
const port = argv.port || 8081;
app.listen(argv.port, ()=>{
console.log('Probably listening to heroku $PORT now ', argv.port); // unless $PORT is undefined, in which case you're listening to 8081.
});
Now when your app starts, it will bind to the dynamically set value of $PORT.