0

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")
6
  • 2
    Do the no static routes work? Commented Jan 24, 2021 at 16:47
  • No they did not I also find that by removing /public from my .gitignore file they started to pop up in GCF. but still GCF is not serving static html,css and giving me 403 error but I think that is completely different question Commented Jan 25, 2021 at 7:21
  • So now, you have a 403 and not a 404? Correct? Do you still use the --allow-unauthenticated parameter? Commented Jan 25, 2021 at 8:13
  • Yes I am still using --allow-unauthenticated I feel I should host static file somewhere else and only use cloud function to host API Commented Jan 25, 2021 at 8:54
  • If I understand correctly, you have deployed new files to public folder and they are not deployed. I tried this on new helloworld deployment and its working fine. I can add and remove files and directories each deployment, there must be somthing wrong with your account... I think you should reach support or try new project Commented Jan 25, 2021 at 14:02

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.