Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

node.js - express server on google cloud functions can not serve static file without trailing /

I hosted express server as google cloud function when ever I hit following link it works perfectly

https://<cloud-function-link>/<project-id>/static/

but if I remove trailing slash / at the end https://<cloud-function-link>/<project-id>/static it redirects to https://<cloud-function-link>/static and give me 403 error

I want it to work with both trailing / and without it.

my app.yaml

runtime: nodejs12

handlers:
  - url: /static
    static_dir: public

  - url: /.*
    script: auto

my index.js

const express = require("express");
const app = express();
const cors = require("cors");
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(
  cors({
    origin: true,
    credentials: true,
    exposedHeaders: ["auth-token"],
  })
);
app.use(cookieParser());
app.use(bodyParser.json());

app.use("/static", express.static("public"));
app.use("/api/auth", authRoute);
app.use("/api/admin", adminRoute);

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

module.exports = {
  app,
}

;

question from:https://stackoverflow.com/questions/65952979/express-server-on-google-cloud-functions-can-not-serve-static-file-without-trail

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...