In these instructions, we will walk you through how to deploy your application using Heroku for backend deployment and surge for frontend deployment. Depending on the complexity of your application, we have provided a series of instructions custom to possible feature variations. These instructions will cover the following situations:
express-api
react-ui
It's important to have this structure because we need two different deployments, one for the front-end and one for the backend.
### Backend
Make sure you are running the following commands in the `express-api` directory for your project.
heroku login
heroku create NAME_OF_APP
Take note of the url that was just created for your new application.
echo "web: node server.js" > Procfile
heroku git:remote -a NAME_OF_APP
These commands will create a web application and the `Procfile` which tells Heroku what command to run to start the server.
git add .
git commit -m "Deploying express api"
git push heroku master
Tada! The backend should now be deployed to Heroku.
### Frontend
Let's now handle deploying our React app. To deploy this repo, we'll be using a tool called **Surge**, which is a very easy way to deploy static websites.
npm install --global surge
Then replace look for all places in your app where you use `http://localhost:3001` as the endpoint. Replace all of them with this variable: `process.env.REACT_APP_REMOTE_HOST_URL`. Here's an example:
const res = await axios.get(`http://localhost:3001/store/${productId}`)
// should become:
const res = await axios.get(`${process.env.REACT_APP_REMOTE_HOST_URL}/store/${productId}`)
Make sure you replace all of them! We'll explain the reason for this in a future lesson.
REACT_APP_REMOTE_HOST_URL=HEROKU_BACKEND_URL npm run build
cp build/index.html build/200.html
surge build
Choose your own domain or let Surge pick one for you.
express-api
react-ui
It's important to have this structure because we need two different deployments, one for the front-end and one for the backend.
### Backend
Make sure you are running the following commands in the `express-api` directory for your project.
heroku login
heroku create NAME_OF_APP
Take note of the url that was just created for your new application.
echo "web: node server.js" > Procfile
heroku git:remote -a NAME_OF_APP
git add .
git commit -m "Deploying express api"
This commands will create a web application and the `Procfile` which tells Heroku what command to run to start the server.
git push heroku master
heroku addons:create heroku-postgresql:hobby-dev -a NAME_OF_APP
This will create a `DATABASE_URL` variable for us that we can then use in the next command. Now we'll go ahead and copy our local database to the production one so that we can have our seed data in production.
heroku pg:push DATABASE_NAME DATABASE_URL -a NAME_OF_APP
heroku open
If you are getting any errors, run `heroku logs -t -a NAME_OF_APP` to attempt to debug them.
heroku config
Then, go ahead and an environment variable update to get our database connection working:
heroku config:set PGSSLMODE=no-verify
All Heroku Postgres production databases require using SSL connections to ensure that communications between applications and the database remain secure, so we're ensuring that our postgres instance doesn't reject our database connection.
npm install --global surge
With the current working directory set to the frontend of your project, run the following commands, replacing `HEROKU_BACKEND_URL` with the url that Heroku provided during the backend deploy process.
REACT_APP_REMOTE_HOST_URL=HEROKU_BACKEND_URL npm run build
cp build/index.html build/200.html
surge build
Choose your own domain or let Surge pick one for you.
express-api
react-ui
It's important to have this structure because we need two different deployments, one for the front-end and one for the backend.
### Backend
Make sure you are running the following commands in the `express-api` directory for your project.
heroku login
heroku create NAME_OF_APP
Take note of the url that was just created for your new application.
echo "web: node server.js" > Procfile
heroku git:remote -a NAME_OF_APP
git add .
git commit -m "Deploying express api"
This commands will create a web application and the `Procfile` which tells Heroku what command to run to start the server.
git push heroku master
heroku addons:create heroku-postgresql:hobby-dev -a NAME_OF_APP
This will create a `DATABASE_URL` variable for us that we can then use in the next command. Now we'll go ahead and copy our local database to the production one so that we can have our seed data in production.
heroku pg:push DATABASE_NAME DATABASE_URL -a NAME_OF_APP
heroku open
If you are getting any errors, run `heroku logs -t -a NAME_OF_APP` to attempt to debug them.
heroku config:set EMAIL_SERVICE_STATUS=active
That should update the `EMAIL_SERVICE_STATUS` to `active` so that our application can send emails. Run `heroku config` again to make sure it's working as expected.
heroku config:set PGSSLMODE=no-verify
All Heroku Postgres production databases require using SSL connections to ensure that communications between applications and the database remain secure, so we're ensuring that our postgres instance doesn't reject our database connection.
npm install --global surge
With the current working directory set to the frontend of your project, run the following commands, replacing `HEROKU_BACKEND_URL` with the url that Heroku provided during the backend deploy process.
REACT_APP_REMOTE_HOST_URL=HEROKU_BACKEND_URL npm run build
cp build/index.html build/200.html
surge build
Choose your own domain or let Surge pick one for you.
heroku config:set EMAIL_FROM_ADDRESS=YOUR_SENDGRID_EMAIL_FROM_ADDRESS
This should match up with your Twilio SendGrid configuration.
heroku config:set CLIENT_URL=YOUR_FRONTEND_URL
This will help when creating links for use by your email service. Make sure not to have a trailing slash on your frontend url.
heroku config:set SENDGRID_API_KEY=YOUR_SENDGRID_API_KEY
This should allow you to start sending emails with Twilio Sendgrid.