Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

trouble deploying node.js app to heroku because of hard coded port number

Tags:

node.js

heroku

I am having trouble deploying the angular-seed app (https://github.com/angular/angular-seed) to Heroku. I believe the problem to be that the port number is hard coded into the server starting scripts when heroku needs to dynamically assign the port through the process.env.PORT variable. Looking through the heroku logs confirms that: Starting up http-server, serving ./ on port: 8000. My Procfile contains web: npm start, and my package.json file contains

"scripts": {
  "prestart": "npm install",
  "start": "http-server -a localhost -p 8000"
}

How can I change that so that my server will still work properly in a local dev environment with foreman start, however dynamically assign a port when using heroku? I want it so that when I type foreman start on my local dev environment, a server will be created at localhost:8000, but when I deploy the app to heroku, the port will be chosen by Heroku.

like image 209
kevin-lin Avatar asked Dec 20 '22 13:12

kevin-lin


1 Answers

I am not a Node developer, but why don't you use the PORT number provisioned by Heroku in your start script?

ie. "start": "http-server -a localhost -p $PORT"

Then it will run on Heroku. If you're running locally, just export PORT=8000, or put PORT=8000 in your .env file if you run using foreman, and you're good to go.

You can also probably add bash magic to set a PORT if none is set by replacing $PORT with ${PORT-8000}.

(Untested)

like image 186
Jon Mountjoy Avatar answered Feb 23 '23 00:02

Jon Mountjoy