找不到 JS - 快速应用 - 云功能

JS not found - express app - cloud functions

我正在使用 Google 云函数和 express js 来提供一些 HTML 文件。我的 js 文件没有显示在浏览器中。它说 404 未找到。

我的文件夹结构

我的 HTML 文件:

    <html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script type="module" src="/public/js/auth.js"></script>
  </head>
  <body>
    <div class="content__container">
      <h1>Hello world</h1>
    </div>
    <script src="/public/js/main.js"></script>
  </body>
</html>

我的app.js

const functions = require("firebase-functions");
const express = require("express");
const path = require("path");


const app = express();

app.use("/js", express.static(path.join(__dirname, "public/js")));

exports.admin = functions.https.onRequest((req, res) => {
  res.sendFile(path.join(__dirname, "public", "admin.html"));
});

调用云函数时浏览器报错admin

函数目录

我想你忘了在你的 js 文件中声明应用程序

const app = express();

在您的 HTML 文件中:

<script src="./js/main.js">

如果 js 是您的 static/public 目录:

<script src="./main.js">

此外,在您的 js 文件中:

res.sendFile(path.join(__dirname, "admin.html"));

您从哪里公开了 Express 应用程序?必须有一个看起来像这样的 HTTP 函数:

const app = express()
app.use("/js", express.static(path.join(__dirname, "public/js")));

exports.api = functions.https.onRequest(app);
// This is missing? or you are not requesting at this URL

添加上述功能后,您的JS文件应该可以在以下URL:

'http://localhost:5001/photography-5b191/us-central-1/api/js/main.js'
// notice the function name ('api' in this case) ---->^^^

您共享的网络日志中的 URL 缺少暴露 Express 应用程序的函数名称。