To deploy a React.js website on Google Cloud, follow these steps:
Step 1: Set up your React app
If you haven’t already created your React app, you can do so using Create React App:
#bash
npx create-react-app my-app
cd my-app
Step 2: Build the React app
React apps need to be built into static files before they can be deployed. Run the following command to generate the production build:
#bash
npm run build
This will create a build
folder with optimized static files.
Step 3: Install Google Cloud SDK
You’ll need the Google Cloud SDK to interact with your Google Cloud account. Download and install the SDK from here.
After installation, initialize the SDK by logging in with your Google account:
#bash
gcloud init
Step 4: Create a Google Cloud project
Create a new project for your React website:
#bash
gcloud projects create my-react-project --set-as-default
Alternatively, you can use the Google Cloud Console to create a project.
Step 5: Enable Firebase Hosting or Cloud Storage
There are two popular methods to host static websites on Google Cloud:
Option 1: Firebase Hosting (recommended)
- Firebase Hosting provides easy deployment for static websites like React apps.
- Install Firebase CLI:
#bash
npm install -g firebase-tools
2. Login to Firebase:
#bash
firebase login
3. Initialize Firebase in your project:
#bash
firebase init
- Choose Firebase Hosting.
- Select your Google Cloud project.
- Choose
build
as the public directory when prompted.
4. Deploy your React app:
#bash
firebase deploy
Option 2: Google Cloud Storage (Static Website Hosting)
- You can use Google Cloud Storage to host static files. Follow these steps:
- Create a new storage bucket:
#bash
gsutil mb gs://my-react-app-bucket
2. Set the bucket to be publicly accessible:
#bash
gsutil iam ch allUsers:objectViewer gs://my-react-app-bucket
3. Copy the React build files to the bucket:
#bash
gsutil -m cp -r build/* gs://my-react-app-bucket
4. In the Google Cloud Console, go to Cloud Storage > Browser, and configure the bucket to serve the static website. Set the Index Page to index.html
.
Step 6: Configure Domain (Optional)
If you have a custom domain, you can point it to your Firebase Hosting or Cloud Storage bucket by updating the DNS settings.
That’s it! Your React.js website should now be live on Google Cloud.
Understanding How Coding Filters Help Reduce Complexity!
Coding filters offer a powerful way to reduce complexity by providing a mechanism to focus on relevant data or logic while ignoring unnecessary elements. By applying these filters, developers can avoid convoluted conditional statements, reducing code length and enhancing clarity in their applications.