Skip to content
All writing

May 19, 2021 · 1 min read

Hosting your Express.js backend on Heroku

Step-by-step guide to deploying an Express.js backend to Heroku using the CLI, including how to push only the backend folder from a monorepo.

#express#hosting#backend

You have created an app in React Native and written backend in Express.js / Node.js and you want to host your backend so you can GET or POST data on your app from anywhere?

Answer: Heroku.

Here's how:

  1. Create a Heroku account on Heroku.
  2. Install the Heroku CLI.
# for mac:
brew tap heroku/brew && brew install heroku
# for ubuntu:
sudo snap install --classic heroku
  1. After the Heroku CLI has been installed, open the terminal and log in with your credentials:
heroku login
  1. Create a Heroku app:
heroku create
  1. Once the Heroku app has been created you will see the app name, remote git link, and the site URL.

  2. Add the Heroku remote branch:

heroku git:remote -a your-app-name
# verify with
git remote -v
  1. Add a Procfile to the root of your backend folder. The Procfile tells Heroku to run node app.js on its server so your app can start.

You don't need to push the whole repo — push only the backend folder:

git subtree push --prefix MainBackendFolder heroku master

After build, Heroku will give you a URL — point your app's API calls at it.