How deploy React app to Heroku using port value from env?

1.1k views Asked by At

Hi I was trying to deploy my rtmp server to Heroku but first I had some issues that I hard coded the ports instead of making env file. I use concurrently to run two scripts. My file structure is

 rtmp-server
 ├── client
 └── rtmp

rtmp needs 2ports (1935 & 8888) & client need 2ports (3000 & 3001) as well

I have few questions.

  1. Do I have to use Dotenv in root or in subdirs (client & rtmp both separately)
  2. How can I use values of Dotenv in package.json
1

There are 1 answers

2
FalseDev On

In your package.json you could do something like command to serve $PORT to get the PORT variable

But there is a better way of handling this by reading the PORT variable in your code like so:

const PORT = process.env.PORT || 3000

Here 3000 is just the fallback value if process.env.PORT is undefined. This is useful while developing