Search⌘ K
AI Features

Getting the Application Port from the Environment

Understand how to override default port settings in your Deno web application using environment variables required by Heroku. This lesson guides you through modifying configuration code, deploying your app in a cloud environment, and testing its functionality, including logging and deployment verification.

Defining ports with Heroku

Heroku has some particularities when it comes to running Docker images. It doesn’t allow us to set the port where the application is running. What it does is assign a port where the application should run, and then redirect HyperText Transfer Protocol (HTTP) and HyperText Transfer Protocol Secure (HTTPS) traffic from the application URL there. If this still feels strange, no worries—we’ll get there.

We explicitly defined the port our application was going to run on in the config.production.yml file. We need to adapt this.

The way Heroku defines which port an application should run on is by setting the PORT environment variable. This is documented here.

We can see from the title what we’re doing next. We’re going to change our application so that the web server port coming from the environment overrides the one defined in the configuration file.

Go back to src/config/index.ts in the application, and make sure it’s reading the PORT variable from the ...