Shashank Padwal
Shashank Padwal

Reputation: 323

gcloud functions deploy not uploading html and css

I created following express API

const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
require("dotenv/config");
//routes
const authRoute = require("./routes/auth.js");
const adminRoute = require("./routes/admin.js");
//middleweres
//converting body into json using body parser
app.use(cookieParser());
app.use(bodyParser.json());
app.use("/", express.static("public"));
app.use("/api/auth", authRoute);
app.use("/api/admin", adminRoute);

// starting express server
// app.listen(5000, () => {
//   console.log("listning on port 5000");
// });

module.exports = {
  app,
};

in public folder I have html file and css and js inside public/static folders html, css and js in public folder are generated with react build. I am trying to deploy this API in google cloud function with following command

gcloud functions deploy outreach-dashboard --entry-point app --runtime nodejs10 --trigger-http --allow-unauthenticated

function is getting deployed but problem is when I see the function on gcp dashboard it source does not contain public folder and if I download source as zip then I can see public folder there but its empty. I need public folder to get deployed so I can serve it using

express.static("public")

Upvotes: 0

Views: 449

Answers (2)

Shashank Padwal
Shashank Padwal

Reputation: 323

As it turns out I have remove public folder from .gitignore and after that also need to tell GCF to treat public folder as static folder by creating app.yaml file in root folder

content of app.yaml

runtime: nodejs12

handlers:
  - url: /static
    static_dir: public

  - url: /.*
    script: auto

Upvotes: 0

guillaume blaquiere
guillaume blaquiere

Reputation: 75715

You are trying to serve several endpoints in the same Cloud Functions. I saw some hack on Stack overflow where folks bend the framework to achieve this. It's not my recommendation.

Cloud Run is a very similar platform. The same underlying infrastructure, and feature very close (I wrote an article on this). But you serve a containerize webserver, more suitable for your use case.

You can easily have a try on it.

  • Uncomment your "starting app express" part
  • Test locally if it works.

Then run this command

gcloud beta run deploy outreach-dashboard --source=. --platform=managed --region=us-central1 --allow-unauthenticated

change the region if needed

The command :

  • upload the sources (take care of your .gitignore and .gcloudignore file to be sure to upload all the files)
  • Create a container with your source. To achieve this, Buildpacks.io is used. Exactly the same process that with Cloud Functions and App Engine.
  • Deploy the container on Cloud Run.

If the problem persist, there is may an issue with the automatic container build process (maybe some file are automatically discarded). In this case, you can write a very simple Dockerfile similar to this one that you have in the getting started documentation.

This time, you can create and deploy in 2 steps (and 2 commands)

# create the container with Cloud Build based on the docker file
gcloud builds submit --tag gcr.io/<PROJECT_ID>/<containerName>

# Deploy on Cloud Run
gcloud beta run deploy outreach-dashboard --image=gcr.io/<PROJECT_ID>/<containerName> --platform=managed --region=us-central1 --allow-unauthenticated

Upvotes: 1

Related Questions