Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

React - Deploying to Heroku

Steps to deploy React app to Heroku

Heroku is a popular cloud platform that makes it easy to deploy, manage, and scale applications. This tutorial covers the steps to deploy your React application to Heroku.

Key Points:

  • Heroku provides a simple and powerful platform for deploying React applications.
  • You can use the Heroku CLI to deploy your application or connect your repository for continuous deployment.
  • Heroku supports custom domains, HTTPS, and environment variables for configuration.

Step 1: Build Your React Application

Before deploying, you need to build your React application. The build process optimizes your application for production:

// Run the build script
npm run build

// The build output will be in the 'build' directory
                

Step 2: Create a Server with Express

Heroku requires a server to serve the static files from the build directory. Create an Express server:

// Install Express
npm install express

// Create a new file named server.js in the root directory and add the following code:
const express = require('express');
const path = require('path');
const app = express();
const PORT = process.env.PORT || 5000;

// Serve static files from the React app
app.use(express.static(path.join(__dirname, 'build')));

// The "catchall" handler: for any request that doesn't
// match one above, send back index.html.
app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

app.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
});
                

Step 3: Create a Procfile

Heroku uses a Procfile to determine how to run your application. Create a Procfile in the root directory and add the following line:

web: node server.js
                

Step 4: Initialize a Git Repository

Initialize a Git repository in your project directory if you haven't already:

// Initialize a new Git repository
git init

// Add all files to the repository
git add .

// Commit the changes
git commit -m "Initial commit"
                

Step 5: Deploy to Heroku Using Heroku CLI

Use the Heroku CLI to deploy your React application:

// Install the Heroku CLI
npm install -g heroku

// Login to Heroku
heroku login

// Create a new Heroku application
heroku create

// Push the code to Heroku
git push heroku master

// Open the application in the browser
heroku open
                

After deployment, Heroku will provide you with a URL where your application is accessible.

Configuring Environment Variables

You can configure environment variables for your Heroku application using the Heroku CLI:

// Set an environment variable
heroku config:set REACT_APP_API_URL=https://api.example.com

// View environment variables
heroku config
                

Setting Up a Custom Domain

Heroku allows you to configure a custom domain and provides free HTTPS with automatic certificate renewal. Follow these steps to set up a custom domain:

  • Step 1: Go to the Heroku dashboard and open your application.
  • Step 2: Go to the "Settings" tab.
  • Step 3: Under "Domains", click on "Add domain".
  • Step 4: Enter your custom domain and follow the instructions to update your DNS settings.

Best Practices for Deployment

Here are some best practices to ensure a smooth deployment:

  • Use environment variables to manage configuration settings for different environments.
  • Enable HTTPS to ensure secure communication between your application and users.
  • Set up automated deployments using CI/CD tools to streamline the deployment process.
  • Monitor your application for performance and errors using monitoring tools.
  • Regularly update dependencies to keep your application secure and up to date.

Summary

In this tutorial, you learned how to deploy a React application to Heroku. Heroku provides a simple and powerful platform for deploying React applications. By following the steps to build your application, create an Express server, configure the Procfile, initialize a Git repository, and deploy using the Heroku CLI, you can ensure a smooth and successful deployment.