Hosting Express Backend On Google Cloud

We already had "Enabled Billing" Simarjot Singh's account using our mentor's credit card. So we decided to host the express app on Google Cloud. The problem was that the google cloud project on which billing was enabled was on Simarjot's account. So he added me as a collaborator in the project and I started the horing process.

  1. Start by creating a Google Cloud account. With this account, you get $300 in free credits, plus free usage of over 20 products, up to monthly limits.

    Create an account

  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Install the Google Cloud CLI.
  5. To initialize the gcloud CLI, run the following command:

    gcloud init
  6. To set the default project for your Cloud Run service:
     gcloud config set project PROJECT_ID
    Replace PROJECT_ID with your Google Cloud project ID.
  7. If you are under a domain restriction organization policy restricting unauthenticated invocations for your project, you will need to access your deployed service as described under Testing private services.

  8. Enable the Cloud Run Admin API and the Cloud Build API:

    gcloud services enable run.googleapis.com \
                    cloudbuild.googleapis.com

    After the Cloud Run Admin API is enabled, the Compute Engine default service account is automatically created.

  9. For Cloud Build to be able to build your sources, grant the Cloud Build Service Account role to the Compute Engine default service account by running the following:

    gcloud projects add-iam-policy-binding PROJECT_ID \
                    --member=serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com \
                    --role=roles/cloudbuild.builds.builder

    Replace PROJECT_NUMBER with your Google Cloud project number, and PROJECT_ID with your Google Cloud project ID. For detailed instructions on how to find your project ID, and project number, see Creating and managing projects.

    Granting the Cloud Build Service Account role to the Compute Engine default service account takes a couple of minutes to propagate.

To create and deploy a Node.js service, follow these steps:

  1. Create a new directory named helloworld and change directory into it:

    mkdir helloworld
    cd helloworld
                    
  2. Create a package.json file with the following contents:

    {
      "name": "helloworld",
      "description": "Simple hello world sample in Node",
      "version": "1.0.0",
      "private": true,
      "main": "index.js",
      "type": "module",
      "scripts": {
        "start": "node index.js"
      },
      "engines": {
        "node": ">=16.0.0"
      },
      "author": "Google LLC",
      "license": "Apache-2.0",
      "dependencies": {
        "express": "^4.17.1"
      }
    }
                    
  3. In the same directory, create a index.js file, and copy the following lines into it:

    import express from 'express';
    const app = express();
    
    app.get('/', (req, res) => {
      const name = process.env.NAME || 'World';
      res.send(`Hello ${name}!`);
    });
    
    const port = parseInt(process.env.PORT) || 8080;
    app.listen(port, () => {
      console.log(`helloworld: listening on port ${port}`);
    });

    This code creates a basic web server that listens on the port defined by the PORT environment variable.

Your app is finished and ready to be deployed.

Important: This quickstart assumes that you have owner or editor roles in the project you are using for the quickstart. Otherwise, refer to the Cloud Run Source Developer role for the required permissions for deploying a Cloud Run resource from source.

Deploy from source automatically builds a container image from source code and deploys it.

To deploy from source:

  1. In your source code directory, deploy the current folder using the following command:

    gcloud run deploy --source .
    1. When you are prompted for the service name, press Enter to accept the default name, for example helloworld.

    2. If you are prompted to enable additional APIs on the project, for example, the Artifact Registry API, respond by pressing y.

    3. When you are prompted for region: select the region of your choice, for example us-central1.

    4. If you are prompted to create a repository in the specified region, respond by pressing y.

    5. If you are prompted to allow unauthenticated invocations: respond y. You might not see this prompt if there is a domain restriction organization policy that prevents it; for more details see the Before you begin section.

    Then wait a few moments until the deployment is complete. On success, the command line displays the service URL.

  2. Visit your deployed service by opening the service URL in a web browser.

Errors That I Encountered

During the initialization step, I was encountering the following error
Error message Basically, it is saying that I do not have the access to activate googleapis for this project from my account. That means I did not have administrative previliges. Even though Simarjot had granted me complete access to the project, this error message kept on showing. Then after so many attempts, one time it suddenly worked and out endpoint is now hosted on https://punjabi-transcript-82115345315.asia-southeast2.run.app/transcript.